日期:2014-05-20  浏览次数:20473 次

MVC 中HttpPost提交,在Control里面的参数是怎么确定的?
最近学习 MVC(linq to entity)
想设计一个话题回复功能,在这涉及到两个表Topic 和Reply,其中Reply中有一个字段为TopicID

我想在页面中实现类似 CSDN ,上面是Topic内容,下面是Reply内容,最下方是一个输入框,输入的内容提交保存到Reply里面


这个页面的强类型用的是 Topic(因为要在上面进行显示)

这里把View代码贴出来
HTML code
@model MvcForum.Models.Topic
@{
    ViewBag.Title = "Details";
}
<style type="text/css">
    hr
    {
        border: 0;
        background-color: #F0F0F0;
        height: 1px;
    }
</style>

<script src="../../Scripts/ckeditor/ckeditor.js" type="text/javascript"></script>
<h2>
   话题</h2>
 
 @*以下内容显示Topic内容*@
@Html.ActionLink("回复","Reply")
<fieldset style="padding: 10px 0px 10px 0px">
    <legend>@Html.DisplayFor(model => model.Subject)</legend>
    <br />
    <div style="float: left;">
        楼主</div>
    <div style="border-width: 1px; text-align: right;">
        发表于: @Html.DisplayFor(model => model.PubTime)
    </div>
    <hr />
    <div style="margin: 20px 20px 20px 20px;">
        @Html.DisplayFor(model => model.Details)
    </div>
    <hr />
    <div style="text-align: right">
        引用|管理|对我有用[0]|丢个板砖[0]|top</div>
</fieldset>

@*以下内容显示Reply内容*@
@{int i = 1;
foreach (var item in Model.Reply.OrderBy(m=>m.ReplyTime)) {
 
    <br />
    <div style="float: left;">@Html.Label(i.ToString())楼</div>
    <div style="border-width: 1px; text-align: right;">
        回复于: @Html.Label(item.ReplyTime.ToString())
    </div>
    <hr />
    <div>@Html.Label(item.ReplyContent)</div>
    <hr />
    <div style="text-align: right">
        引用|管理|对我有用[0]|丢个板砖[0]|top</div>
    i++;
}}


@*通过一个按钮做提交回复,不过这里无法提交,因为在Control里面无法得到Reply类*@
@using (@Html.BeginForm())
{
    MvcForum.Models.Reply reply = new MvcForum.Models.Reply();
    @Html.DisplayFor(model => model.TopicID);
    <textarea class="ckeditor"  id="editor1" name="editor1" cols="100" rows="10">@Html.DisplayFor(model => model.Subject)</textarea>
    <input type="submit" value="提交回复"  />
}


问题:
1、这里我想问一下,我这样做对不对?
2、@using (@Html.BeginForm()){}这个里面是提交表格,在下面代码中CreateReply的参数是根据什么决定的,为什么只有一个强类型(创建User)时候,这里可以通过 ActionResult CreateUser(User user)来获取到 user,这这里不行
  [HttpPost]
  public ActionResult CreateReply(Reply reply)
  {
  //db.Reply.AddObject(reply);
  //db.SaveChanges();
  return View();
  }

不吃饭,坐等

------解决方案--------------------
真的等啊?我不明白在问什么
------解决方案--------------------
这样做可以,不过
MvcForum.Models.Reply reply = new MvcForum.Models.Reply();
@Html.DisplayFor(model => model.TopicID);
<textarea class="ckeditor" id="editor1" name="editor1" cols="100" rows="10">@Html.DisplayFor(model => model.Subject)</textarea>
<input type="submit" value="提交回复" />
这里面好像写的有问题,我建一个例子研究一下看看
------解决方案--------------------
神奇的疑问,考虑中.................
------解决方案--------------------
cotroller中要写一个
[httppost]
public ActionResult Reply(int id)
你最好看看www.asp.net 中MVC的MvcMusicStore例子
------解决方案--------------------