Saturday, 15 June 2013

c# - MVC Model is null in HTTPOST -



c# - MVC Model is null in HTTPOST -

there's number of questions already, despite looking on quite few of them i'm having problem seeing problem here. when submit below form, model null. suggestions on i'm doing wrong here?

model (partial):

public class itemheader { public string hdr_contact_name { get; set; } public string hdr_contact_method { get; set; } }

controller:

public actionresult details(int? id) { if (id == null) { homecoming new httpstatuscoderesult(httpstatuscode.notfound); } int id2 = (int)id; db db = new db(); itemheader itemheader = db.getheader(id2); if (itemheader == null) { homecoming new httpstatuscoderesult(httpstatuscode.notfound); } else { homecoming view(itemheader); } } [httppost] public actionresult details(itemheader itemheader) { string test; db db = new db(); response.write(db.updateheader(itemheader)); response.end(); homecoming null; }

view (partial):

@model mvctest2.models.itemheader @{ html.enableclientvalidation(true); } @using (html.beginform()) { <table> <tr> <td>vendor contact</td> <td>@html.textboxfor(model => model.hdr_contact_name, new { style = "width:24em", maxlength = 40 })</td> <td>email/phone</td> <td>@html.textboxfor(model => model.hdr_contact_method, new { style = "width:24em", maxlength = 40 })</td> </tr> </table> <input type="submit" value="submit" /> }

html (partial):

<div> <form action="/mvctest2/vendoritem/details/69" method="post"> <h4>item request details</h4> <hr /> <table> <tr> <td>vendor contact</td> <td><input id="hdr_contact_name" maxlength="40" name="hdr_contact_name" style="width:24em" type="text" value="" /></td> <td>email/phone</td> <td><input id="hdr_contact_method" maxlength="40" name="hdr_contact_method" style="width:24em" type="text" value="" /></td> </tr> </table> <p> <input type="submit" value="submit" /> &nbsp;&nbsp; </p> </form></div>

the form posting /mvctest2/vendoritem/details/69 action method post details not have id parameter.

try using instead of have:

[httppost] public actionresult details(int? id, itemheader itemheader) { string test; db db = new db(); response.write(db.updateheader(itemheader)); response.end(); homecoming null; }

c# model-view-controller

No comments:

Post a Comment