views:

57

answers:

2

I have the following actions in my controller. The first (top) Edit works fine and serves the correct values. However, in the second one, i.e. the 'return' Edit, every property of model is at default value, i.e. null for ref types and zero for value types. I have examined the HTTP post data and it has all the properties correctly named and with correct values. What could be wrong?

Controller excerpt:

        [Authorize(Order = 0, Roles = "Requester, Controller")]
        public ActionResult Edit(int id)
        {
            JobCardViewData viewData = ViewDataFactory.CreateBaseViewData<JobCardViewData>("Installation Details");
            viewData.JobCard = new JobCardService().GetById(id);
            return View(viewData);
        }

        [HttpPost]
        public ActionResult Edit(JobCard model)
        {
            try
            {
                new JobCardService().Update(model);
                var x = RedirectToAction("Index");
                return RedirectToAction("Index");
            }
            catch (Exception)
            {

                return RedirectToAction("Edit", new {id = model.InstallationNumber});
            }
        }

View excerpt:

        <div class="editor-label">
            <%: Html.LabelFor(model => model.JobCard.Name) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.JobCard.Name) %>
            <%: Html.ValidationMessageFor(model => model.JobCard.Name) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.JobCard.Surname) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.JobCard.Surname) %>
            <%: Html.ValidationMessageFor(model => model.JobCard.Surname) %>
        </div>
A: 

Can you examine the contents of the raw HTTP POST using Fiddler (or anything else) and confirm the values are actually getting sent to the web server? Is this a problem of getting values to the server or binding the values to your object?

AndrewDotHay
@Andrew, You are the second person to recommend that, I will try it shortly, thanks, but regards the binding, I'm using 'TextBoxFor', not the 'Bind' method, so I have no idea if the problem is with binding.
ProfK
@Andrew, please see my edits. The post (using Firebug) contains all correct data.
ProfK
+2  A: 

Refer to this answer from Phil http://stackoverflow.com/questions/2160171/using-viewmodel-pattern-with-mvc-2-strongly-typed-html-helpers/2172756#2172756

When you use the strongly typed helpers against the type, the helpers create form fields assuming that's the type you're posting to. When the types don't match up, there's a problem.

In this case, the type being posted to (JobCard) is different to the type the view originally was created against(JobCardViewData).

Try changing the method signature to the ffg and I am almost certain the JobCard values will be populated within the model.

[HttpPost]
public ActionResult Edit(JobCardViewData model)
{ ... }

Phil presents two approaches which can be used to resolve this.

Ahmad
http://stackoverflow.com/questions/2494940/custom-viewmodel-with-mvc-2-strongly-typed-html-helpers-return-null-object-on-cre/2500327#2500327
Ahmad
Ha! You're exactly right. I stared at that code for about 15 minutes and I didn't see it. The data is getting to the server, but it's not in a format that can automatically bind to the parameter in the action method.
AndrewDotHay