ASP.NET MVC 4 Create method has null object -
i have problem creating object don't know why. have null object in parameter in post create method , in modelstate error:
{system.invalidoperationexception: parameter conversion type 'system.string' type 'blanskomenu.models.actionoffer' failed because no type converter can convert between these types. @ system.web.mvc.valueproviderresult.convertsimpletype(cultureinfo culture, object value, type destinationtype) @ system.web.mvc.valueproviderresult.unwrappossiblearraytype(cultureinfo culture, object value, type destinationtype) @ system.web.mvc.valueproviderresult.convertto(type type, cultureinfo culture) @ system.web.mvc.defaultmodelbinder.convertproviderresult(modelstatedictionary modelstate, string modelstatekey, valueproviderresult valueproviderresult, type destinationtype)}
these methods in controller:
// // get: /action/ public actionresult index() { var offers = _repository.getallactionoffers(); return view(offers); } // // get: /action/create public actionresult create() { return view(); } // // post: /action/create [httppost] public actionresult create(actionoffer action) { try { if (modelstate.isvalid) { _repository.createactionoffer(action); return redirecttoaction("index"); } return view(action); } catch { return view(action); } }
this actionoffer model:
public class actionoffer { [key] public int id { get; set; } public string text { get; set; } public string title { get; set; } public string imagepath { get; set; } [datatype(datatype.date)] public datetime startdate { get; set; } [datatype(datatype.date)] public datetime enddate { get; set; } }
and create view:
@model blanskomenu.models.actionoffer @{ viewbag.title = "vytvořit akční nabídku"; layout = "~/views/shared/_layout.cshtml"; } <h2>vytvořit akční nabídku</h2> @using (html.beginform()) { @html.antiforgerytoken() @html.validationsummary(true) <fieldset> <legend>akční nabídka</legend> @html.hiddenfor(model => model.id) <div class="editor-label"> @html.labelfor(model => model.title) </div> <div class="editor-field"> @html.editorfor(model => model.title) @html.validationmessagefor(model => model.title) </div> <div class="editor-label"> @html.labelfor(model => model.text) </div> <div class="editor-field"> @html.editorfor(model => model.text) @html.validationmessagefor(model => model.text) </div> <div class="editor-label"> @html.labelfor(model => model.imagepath) </div> <div class="editor-field"> @html.editorfor(model => model.imagepath) @html.validationmessagefor(model => model.imagepath) </div> <div class="editor-label"> @html.labelfor(model => model.startdate) </div> <div class="editor-field"> @html.editorfor(model => model.startdate) @html.validationmessagefor(model => model.startdate) </div> <div class="editor-label"> @html.labelfor(model => model.enddate) </div> <div class="editor-field"> @html.editorfor(model => model.enddate) @html.validationmessagefor(model => model.enddate) </div> <p> <input type="submit" value="save" /> </p> </fieldset> } <div> @html.actionlink("back list", "index") </div> @section scripts { @scripts.render("~/bundles/jqueryval") }
thanks help
try changing
public actionresult create(actionoffer action)
to
public actionresult create(actionoffer actionoffer)
Comments
Post a Comment