views:

502

answers:

2

I am trying to refactor to avoid parsing the FormCollection from the view so i changed this to pass in an strongly typed object. My form elements are the same names as the properties on the LinkUpdater Object. But when i put a breakpoint on the first link in the controller all of the properties are null.

any ideas or suggestions?

View:

 <%using (Ajax.BeginForm("AddNewLink", "Links", new AjaxOptions { UpdateTargetId = "LinkList", LoadingElementId = "updating", OnSuccess = "done" }))
  { %>

 <fieldset style="text-align:left">
 <table>
 <tr><td>Url:</td><td> <input style="width:500px" type="text" name="URL" /></td></tr>
 <tr><td>Description: </td><td><input style="width:400px" type="text" name="Description" /></td></tr>
 <tr><td>Tags: </td><td><input style="width:400px" id="Tags" name="tags" type="text" /></td></tr>
 <tr><td><input type="submit" value="Add Link" name="submit" /></td></tr>
 </table>
 </fieldset>
  <% } %>

Controller Post:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult AddNewLink(LinkUpdater linkUpdater_)
    {
        string[] tags = linkUpdater_.Tags.Replace(" ", "").Split(',');
        linkRepository.AddLink(linkUpdater_.URL, linkUpdater_.Description, tags);
        .....
    }

LinkUpdater class:

public class LinkUpdater
{
    public string URL;
    public string Description;
    public string Tags;
}
A: 

Is there any particular reason your are not using the strongly-typed HTMLHelpers to render your input fields?

<%using (Ajax.BeginForm("AddNewLink", "Links", new AjaxOptions { UpdateTargetId = "LinkList", LoadingElementId = "updating", OnSuccess = "done" }))
  { %>
<fieldset style="text-align: left">
 <table>
  <tr>
   <td>
    Url:
   </td>
   <td>
    <%=Html.TextBox("URL", Model.URL, new { style = "width:500px;" }) %>
   </td>
  </tr>
  <tr>
   <td>
    Description:
   </td>
   <td>
    <%=Html.TextBox("Description", Model.Description, new { style = "width:400px;" }) %>
   </td>
  </tr>
  <tr>
   <td>
    Tags:
   </td>
   <td>
    <%=Html.TextBox("Tags", Model.Tags, new { style = "width:400px;" }) %>
   </td>
  </tr>
  <tr>
   <td>
    <input type="submit" value="Add Link" name="submit" />
   </td>
  </tr>
 </table>
</fieldset>
<% } %>

I'm not sure it will fix your problem, but it's a step in the right direction at least.

Nathan Taylor
yes . . this is because the data that i am sending back is not the model that i binding the full view to . . maybe thats the issue . . the full page view binds to a larger object. do these have to be the same thing??
ooo
That shouldn't make a difference, the default ModelBinder doesn't care about how the fields were created, just that the names and ids match properties in the bound object. Still, HTMLHelpers help keep the code more flexible and type safe. Did queen3's solution work for ya?
Nathan Taylor
+4  A: 

Model binder in MVC binds to properties, while you use fields. Change to

public string URL { get; set; }

And by the way, there're other drawbacks, like if you use private set, it will silently skip binding, too.

queen3