我在 mvc3 中遇到问题。我不确定这是否是专门的 mvc3,但是我目前正在将其与 razor 引擎一起使用。不管怎样,我面临的问题是,我有一个表单,我使用 TextBoxFor、CheckBoxFor 等来渲染它。渲染工作完美无缺,除了当我尝试发布数据时,我基本上发布了一个带有空值的空表单。
这是我的模型:
public class SendReplyPmForm : PM
{
public new string Text { get; set; }
public bool IsOriginalDelete { get; set; }
public int ReplyNr { get; set; }
public string ReceiverName { get; set; }
}
我在 View 和模型之间有一个额外的 View 模型层,它包含有关该模型的额外参数
public class IndexViewModel
{
public SendReplyPmForm SendReplyPmForm { get; set; }
...
这是我的观点
@using (Html.BeginForm("SendReply", "Pm", FormMethod.Post, new { id = "formSendMsg" }))
{
@Html.TextBoxFor(model => model.SendReplyPmForm.ReceiverName, new { id = "ReceiverName" })
<span id="spanChkText">Delete Original Message: @Html.CheckBoxFor(model => model.SendReplyPmForm.IsOriginalDelete, new { id = "chkIsOriginalDelete", value = 1 })</span>
@{Html.RenderPartial("~/Areas/Forums/Views/Shared/Toolbar.cshtml");}
<span class="spanLabel">Message</span>
@Html.TextAreaFor(model => model.SendReplyPmForm.Text, new { id = "Text", rows = "10", cols = "65" })
@{Html.RenderPartial("temp.cshtml");}
@Html.HiddenFor(model => model.SendReplyPmForm.ReplyNr, new { id = "inputReplyNr", value = 0 })
<input type="submit" value="Send" />
}
这里我有 Controller
[HttpPost]
public ActionResult SendReply(SendReplyPmForm SendReplyForm) {
var ViewModel = new IndexViewModel();
.
.
.
return View("Index", ViewModel);
}
奇怪的是,如果我使用纯 HTML 而不是 Html 助手,那么帖子就会顺利进行,没有任何问题。
在发布本文之前,我读过这篇文章( ASP.NET MVC’s Html Helpers Render the Wrong Value! ),但我不太确定我是否有同样的问题。 (例如:我的 Controller 中没有同名的操作),但事实上它也与纯 Html 一起使用,这让我思考。
你们知道如何将这个表单与 Html 助手一起使用吗?
最佳答案
我相信您的问题与 here 相同,所以需要添加前缀:
[HttpPost]
public ActionResult SendReply([Bind(Prefix="SendReplyPmForm")]SendReplyPmForm SendReplyForm)
{
...
}
关于c# - 使用 html 助手时没有发布值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4861143/