views:

57

answers:

1

I'm working on my very first ASP.NET MVC project, and I'm seeing strange behavior when returning data from an Edit View to my controller action.

Right now, I have 3 textboxes on the page, plus one hidden one for the PKey. All are being populated from the viewdata correctly, but when I submit the form, only 2 of the 3 fields show up in the returned model. However, all three fields are populated correctly in the Request Object.

I'm probably not explaining it very well, but here's some pertinent code snips to hopefully better explain:

    public ActionResult Edit(System.Guid Id)
    {

        SetBase sb = setBaseRepository.Get(Id);
        return View("Edit", sb );
    }
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(SetBase setBase)
    {
        //if (setBase.Title.Trim().Length == 0)
        //{
        //    ModelState.AddModelError("Title", "Title is required.");
        //}
        if (setBase.Year.Trim().Length == 0) 
        {
            ModelState.AddModelError("Year", "Year is required.");
        }
        if (!ModelState.IsValid)
        {
            return View("Edit", setBase);
        }
        setBaseRepository.SaveOrUpdate(setBase);
        return View();
    }

And here's the 'meat' of the View Itself:

<viewdata model="CardTracker.Core.SetBase">
<content name="MainContent">
<% MvcForm form = Html.BeginForm("Edit", "SetBase", Model.Id); %>

<%=Html.ValidationSummary("Update was unsuccessful. Please correct the errors and try again.", new { class = "dataEntryError" })%>

<fieldset>
    <legend class="dataEntry">Edit Set Base</legend>   
<div>
!{Html.Hidden("Id")}
<label class="dataEntry" for="Title" >Title: </label> ${Html.TextBox("Title", Model.Title, new { class = "dataEntryLong" })}
<%=Html.ValidationMessage("Title", "***", new { class = "dataEntryError" })%>
</br>

<label class="dataEntry" for="Year">Year:</label>${Html.TextBox("Year", null, new { class = "dataEntryNumber" })}
<%=Html.ValidationMessage("Year", "***", new { class = "dataEntryError" })%>
</br>


</div>
<input type="submit" value="Update" class="button" /> 
</fieldset>
<% form.EndForm(); %>
</content>

The 'Id' and 'Year' fields are returned just fine, but 'Title' always comes back blank. I have verified that all of them are spelled correctly everywhere.

I'm sure I'm doing something obviously wrong, but I don't see it. The numerous examples I have studied aren't helping, and most of them show Add functionality instead of Edit.

Thanks in advance for any and all help.

A: 

I'm going to start listing the things I notice until perhaps I stumble upon something that really helps you out:

  1. The Title field is the only field that actually references the model. Your Year field has null for the initial value. I don't really think this matters, but it is the only difference I can see.
  2. Just trying to find the odd edge cases here, so I'm making sure that the "SetBase" value in the BeginForm("Edit","SetBase", Model.id) statement is indeed calling the controller? That is the same name as your model, which obviously is fine, but wanted to make sure it that is the name of the controller.

...everything looks OK, but obviously something is wrong.

Nick DeVore
Yeah, that was an attempt to fix things. It still fails when I replace that with null.
B King
Added #2 above. I don't think this is the problem really, but thought I'd bring it up.
Nick DeVore
Yes, it is the proper controller name. I'm sure because I do hit the breakpoint in the desired ActionMethod. Good suggestion though.
B King
This may be more work than you're willing to do, but perhaps you try a "normal" MVC view instead of using spring just to see if you can get it to work there. If so, then perhaps that might give you the clue for why its failing in your spring implementation?
Nick DeVore
Probably not a bad idea. I'll give that a whirl.
B King