c# - Create View with Multiple Models and Forms with Validation - MVC 5 -
how can create view has multiple models need validation , has multiple forms submit within same view?
i have solution below. typing able figure out, else. if others have better approach or comments, please feel free post!
i have view contains viewmodel, combines 2 models. here viewmodel, along other classes:
public class playerformviewmodel { public playerformenglish playerformenglish { get; set; } public playerformspanish playerformspanish { get; set; } } public class playerformenglish : playerforminformation { } public class playerformspanish : playerforminformation { } public class playerforminformation { [required(errormessage = "first name required field.")] [display(name = "first name")] public string firstname { get; set; } // used dropdown in view [display(name = "gender")] public ienumerable<selectlistitem> gender{ get; set; } }
my main view contains viewmodel , 2 forms:
@model namespace.models.playerformviewmodel @using (html.beginform("playertest", "profile")) { @html.antiforgerytoken() <h3>english</h3> @html.labelfor(m => m.playerformenglish.firstname) @html.textboxfor(m => m.playerformenglish.firstname) @html.validationmessagefor(m => m.playerformenglish.firstname) @html.labelfor(m => m.playerformenglish.gender) @html.dropdownlistfor(m => m.playerformenglish.gender, new selectlist(model.playerformenglish.gender, "value", "text")) <button type="submit" class="btn btn-default" name="buttontype" value="saveenglishform">save</button> } @using (html.beginform("playertest", "profile")) { @html.antiforgerytoken() <h3>spanish</h3> @html.labelfor(m => m.playerformspanish.firstname) @html.textboxfor(m => m.playerformspanish.firstname) @html.validationmessagefor(m => m.playerformspanish.firstname) @html.labelfor(m => m.playerformspanish.gender) @html.dropdownlistfor(m => m.playerformspanish.gender, new selectlist(model.playerformenglish.gender, "value", "text")) <button type="submit" class="btn btn-default" name="buttontype" value="savespanishform">save</button> }
when page first loads, pre-populate fields this:
[httpget] public actionresult playertest() { playerformviewmodel model = new playerformviewmodel(); model.playerformenglish = new playerformenglish(); model.playerformspanish = new playerformspanish(); model.playerformenglish.firstname = "brad"; list<someobject> genderlist = getdatafordropdown(); model.playerformenglish.gender = convertdata(genderlist); model.playerformspanish.gender = convertdata(genderlist); return view(model); }
finally, validate , update saved form when user clicks save button:
[httppost] [validateinput(false)] [validateantiforgerytoken] public actionresult playertest(playerformviewmodel model, string buttontype) { if (buttontype.equals("saveenglishform")) { if (modelstate.isvalid) { return redirecttoaction("successfulsave", "profile"); } model.playerformspanish = new playerformspanish(); } else { if (modelstate.isvalid) { return redirecttoaction("successfulsave", "profile"); } model.playerformenglish = new playerformenglish(); } // can repopulate fields here before model.playerformenglish.firstname = "brad"; list<someobject> genderlist = getdatafordropdown(); model.playerformenglish.gender = convertdata(genderlist); model.playerformspanish.gender = convertdata(genderlist); // return model mark required fields in ui return view(model); }
hope helps!
Comments
Post a Comment