简体   繁体   English

复杂模型在发布后保持为空

[英]complex model stays null after post

My question is related to this question and answer 我的问题与这个问题和答案有关

The following complex model: 以下复杂模型:

public class EditSubmissionModel
{
    public string foo { get; set; }
    public Submission submission { get; set; }
}

The simple model 简单的模型

[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; }
}

The view: 风景:

@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" />
}

the editorTemplate editorTemplate

@model Project.Domain.Entities.Submission
@Html.EditorFor(m => m.Title)

the controller 控制器

    [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?)

I can't see the error, any ideas? 我看不出错误,有什么想法吗?

Status no repro. 状态无重复。 Steps: 脚步:

  1. Create a new ASP.NET MVC 3 application using the default template 使用默认模板创建新的ASP.NET MVC 3应用程序
  2. Define 2 models: 定义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; } } 
  3. Modify HomeController so that it looks like this: 修改HomeController ,使其如下所示:

     public class HomeController : Controller { public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(string shortName, EditSubmissionModel model) { return Content(model.submission.Title); } } 
  4. Update ~/Views/Home/Index.cshtml view so that it looks like this: 更新~/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" /> } 
  5. Add a custom editor template for the Submission type ( ~/Views/Home/EditorTemplates/.cshtml ) like this: Submission类型添加自定义编辑器模板( ~/Views/Home/EditorTemplates/.cshtml ),如下所示:

     @model Submission @Html.EditorFor(m => m.Title) 
  6. Hit Ctrl+F5 , fill in the form and submit. 按Ctrl + F5 ,填写表格并提交。 As totally expected the value you have entered in the Title textbox will be correctly bound and shown on the screen. 完全预期,您在Title文本框中输入的值将被正确绑定并显示在屏幕上。

So I repeat the question that I've already asked you in the comments section: what did you do differently? 所以我重复一下我在评论部分已经问你的问题:你做了哪些不同的事情? You answered that it is a copy-paste from your code, but as I have illustrated you (with a full step-by-step guide) this is not the case. 您回答说它是代码中的复制粘贴,但正如我已经说明的那样(带有完整的分步指南),情况并非如此。

Now here's a suspicion that I have. 现在我怀疑自己。 Your actual POST action looks like this: 您的实际POST操作如下所示:

public ActionResult editSubmission(string shortName, EditSubmissionModel submission)

and not like this: 而不是这样的:

public ActionResult editSubmission(string shortName, EditSubmissionModel model)

Notice the parameter name. 注意参数名称。

@Darin Dimitrov you were completely right, what do i do different. @Darin Dimitrov你完全正确,我做了什么不同。 The code above was completely fine. 上面的代码完全没问题。 The problem was the get command which looked like: 问题是get命令看起来像:

[Authorize]
public ActionResult editSubmission(string confShortName, string submission)
{
  //do stuff
  return View();
}

And the Modelbinder will get problems if the httpPost anywhere has same name like the HttpGet in my case string submission and Editsubmission.submission. 如果httpPost在我的案例字符串提交和Editsubmission.submission中的HttpGet具有相同的名称,那么Modelbinder会出现问题。 Big thanks to your detailed step by step advice! 非常感谢您详细的一步一步的建议!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM