0

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

@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?

Community
  • 1
  • 1
Vulcano
  • 415
  • 10
  • 25

2 Answers2

2

Status no repro. Steps:

  1. Create a new ASP.NET MVC 3 application using the default template
  2. Define 2 models:

    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:

    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:

    @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:

    @model Submission
    @Html.EditorFor(m => m.Title)
    
  6. Hit Ctrl+F5, fill in the form and submit. As totally expected the value you have entered in the Title textbox will be correctly bound and shown on the screen.

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:

public ActionResult editSubmission(string shortName, EditSubmissionModel submission)

and not like this:

public ActionResult editSubmission(string shortName, EditSubmissionModel model)

Notice the parameter name.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • well i tried your step by step advice and... it works, so its interesting that i kinda can't reproduce my own error. Concerning your suspicion its really model – Vulcano Jul 02 '12 at 09:10
  • Here's what I will suggest you: start dumping out parts of your code until you narrow it down. Another approach is to start from the working version I've showed you and progressively enhance it with other parts of your code until you narrow it down. Unfortunately I cannot help you without knowing what code you might have written. – Darin Dimitrov Jul 02 '12 at 09:11
  • currently on narrowing, but it was already a huge help that it _should_ work. – Vulcano Jul 02 '12 at 09:16
  • update: i compressed until everything looked the same and still nothing. but i noticed in the call stack there is an exception after the post and before the breakpoint. parameter conversion from type sys.string > submission failed because no type converter. so it seems the modelbinder cant bind the textbox to the submission. but why he can't do it, i don't know yet. Any suggestions? – Vulcano Jul 02 '12 at 10:06
0

@Darin Dimitrov you were completely right, what do i do different. The code above was completely fine. The problem was the get command which looked like:

[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. Big thanks to your detailed step by step advice!

Vulcano
  • 415
  • 10
  • 25