c# - Why does a submit button pass null data from view to controller -
i'm new c# , mvc , may basic question, after thorougly searching people simmilar problems can't make work.
the problem on submit button have on edit view reason pass null data update method instead of form input data.
this view:
<div class="row-fluid metro"> <div class="span2 offset4"> @using (html.beginform("update", "realty", formmethod.post)) { @html.hiddenfor(model => model.address) @html.hiddenfor(model => model.details) @html.hiddenfor(model => model.manager) @html.validationsummary(true) <div class="control-group"> <label class="control-label">details</label> <div class="controls"> @html.editorfor(model => model.details) @html.validationmessagefor(model => model.details) </div> </div> <div class="control-group"> <label class="control-label">address</label> <div class="controls"> @html.editorfor(model => model.address) @html.validationmessagefor(model => model.address) </div> </div> <div class="control-group"> <label class="control-label">manager</label> <div class="controls"> @html.dropdownlistfor(model => model.manager.id, model.managers) </div> </div> <div class="form-actions"> <button type="submit" class="btn btn-primary">guardar</button> <a href="@url.action("index", "realty")"><button type="button" class="btn">cancelar</button></a> </div> } </div>
this viewmodel public class realtyviewmodel { /// /// gets or sets id. /// public int id { get; set; }
public string address { get; private set; } /// <summary> /// gets details. /// </summary> public string details { get; private set; } public list<homeviewmodel> homes { get; private set; } /// <summary> /// gets manager. /// </summary> public ienumerable<selectlistitem> managers { get; set; } public managerviewmodel manager { get; private set; } public realtyviewmodel(int id,string address, string details, managerviewmodel manager) { this.id = id; this.address = address; this.details = details; this.manager = manager; } public realtyviewmodel() {} }
}
and finnally, update method controller if needed:
public actionresult update(realtyviewmodel model) { if (model.id == 0) { var manager = this.managerservice.get(model.manager.id); this.realtyservice.create(model.address, model.details,manager); } else { var manager = this.managerservice.get(model.manager.id); this.realtyservice.update(model.id,model.address, model.details, manager); } return this.redirecttoaction("index"); }
any appreciated, in advance.
public actionresult edit(int id) { list<selectlistitem> managers = new list<selectlistitem>(); var model = new realtyviewmodel(); if (id != 0) { var realty = this.realtyservice.get(id); var mvm = new managerviewmodel(realty.manager.id, realty.manager.name, realty.manager.age); model = new realtyviewmodel(realty.id, realty.address, realty.details, mvm); } foreach (var manager in managerservice.getall()) { managers.add(new selectlistitem { value=manager.id.tostring(),text=manager.name}); } model.managers = managers; return this.view(model); }
try remove hidden fields form
<div class="row-fluid metro"> <div class="span2 offset4"> @using (html.beginform("update", "realty", formmethod.post)) { @html.validationsummary(true) <div class="control-group"> <label class="control-label">details</label> <div class="controls"> @html.editorfor(model => model.details) @html.validationmessagefor(model => model.details) </div> </div> <div class="control-group"> <label class="control-label">address</label> <div class="controls"> @html.editorfor(model => model.address) @html.validationmessagefor(model => model.address) </div> </div> <div class="control-group"> <label class="control-label">manager</label> <div class="controls"> @html.dropdownlistfor(model => model.manager.id, model.managers) </div> </div> <div class="form-actions"> <button type="submit" class="btn btn-primary">guardar</button> <a href="@url.action("index", "realty")"><button type="button" class="btn">cancelar</button></a> </div> } </div>
Comments
Post a Comment