以下复杂模型:
public class EditSubmissionModel
{
public string foo { get; set; }
public Submission submission { get; set; }
}
简单模型
[Table(Name = "Submission")]
public class Submission
{
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
public int SubmissionId { get; set; }
[Column]
public string Title { get; set; }
}
观点:
@model Project.WebUI.Models.EditSubmissionModel
@{
ViewBag.Title = "editSubmission";
}
<h2>editSubmission</h2>
@using (Html.BeginForm())
{
<legend>SubmissionModel</legend>
@Html.EditorFor(m => m.foo)
@Html.EditorFor(m => m.submission)
<input type="submit" value="Save" />
}
editorTemplate
@model Project.Domain.Entities.Submission
@Html.EditorFor(m => m.Title)
控制器
[Authorize]
[HttpPost]
public ActionResult editSubmission(string shortName, EditSubmissionModel model)
{
shortname = "second" (is ok)
model.foo = aaa (also ok i edited it on the view)
model.submission = null (not binded? or i dont know?)
我看不出错误,有什么想法吗?
答案 0 :(得分:2)
状态无重复。步骤进行:
定义2个模型:
public class Submission
{
public int SubmissionId { get; set; }
public string Title { get; set; }
}
public class EditSubmissionModel
{
public string foo { get; set; }
public Submission submission { get; set; }
}
修改HomeController
,使其如下所示:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string shortName, EditSubmissionModel model)
{
return Content(model.submission.Title);
}
}
更新~/Views/Home/Index.cshtml
视图,使其如下所示:
@model EditSubmissionModel
@using (Html.BeginForm())
{
<legend>SubmissionModel</legend>
@Html.EditorFor(m => m.foo)
@Html.EditorFor(m => m.submission)
<input type="submit" value="Save" />
}
为Submission
类型(~/Views/Home/EditorTemplates/.cshtml
)添加自定义编辑器模板,如下所示:
@model Submission
@Html.EditorFor(m => m.Title)
按 Ctrl + F5 ,填写表格并提交。完全符合预期,您在Title
文本框中输入的值将被正确绑定并显示在屏幕上。
所以我重复一下我在评论部分已经问你的问题:你做了哪些不同的事情?您回答说它是代码中的复制粘贴,但正如我已经向您说明的那样(有完整的分步指南),情况并非如此。
现在怀疑我有。您的实际POST操作如下所示:
public ActionResult editSubmission(string shortName, EditSubmissionModel submission)
而不是这样:
public ActionResult editSubmission(string shortName, EditSubmissionModel model)
注意参数名称。
答案 1 :(得分:0)
[Authorize]
public ActionResult editSubmission(string confShortName, string submission)
{
//do stuff
return View();
}
如果httpPost在我的案例字符串提交和Editsubmission.submission中的HttpGet同名,那么Modelbinder会出现问题。非常感谢您详细的一步一步的建议!