读书人

MVC 中HttpPost提交在Control里面的

发布时间: 2011-12-27 22:22:54 作者: rapoo

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例子
------解决方案--------------------


回复你的问题: 在下面代码中CreateReply的参数是根据什么决定的

根据name=名和你的对象里面的属性名相当来匹配的.
[解决办法]
@using (@Html.BeginForm()){}
好像将所有填写内容收集
一同传给reply(replyModel model)
看看下面的例子:
@using (Html.BeginForm()) {
<div>
<fieldset>
<legend>Account Information</legend>

<div class="editor-label">
@Html.LabelFor(m => m.UserName)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div>

<div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>

<div class="editor-label">
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
</div>

<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
}
controller中

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
MigrateShoppingCart(model.UserName);
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}

// If we got this far, something failed, redisplay form
return View(model);
}
[解决办法]
@using (@Html.BeginForm()) 这个里边写上控制器和action的名字,你在看下
[解决办法]
把[HttpPost]换成[AcceptVerbs(HttpVerbs.Post)]
[解决办法]
你可以做的跟winform一样,request.from["name"] 这么接收值啊
[解决办法]

HTML code
<%Html.BeginForm("Report", "Admin",new{id=1}, FormMethod.Post);%><!--                                                                                           --> [HttpPost]        [SessionFilterAttribute(SessionFilterName.Admin)]        public ActionResult Report(int id)        {            return null;        } 

读书人网 >asp.net

热点推荐