c# - Using RenderPartial to assign Model Property values -
i new mvc platform , trying simple application hold of framework,
application:i designing admin application has form enter question , multiple options database, have viewmodel called "questionviewmodel" has properties "quesiton" , list of optionsviewmodel in it, "optionviewmodel" consists of "option" , "isrightanswer" properties, on ui, have questionview displays textbox question , want question have 4 options, created partialview called "questionoptionview" takes "optionviewmodel" model, loop 4 times through partialview , display textbox "option" , "isrightasnwer" radiobutton,
the ui displays tetsbox "question" , 4 other "textboxes" entering "options", when post form, "options" not bind viewmodel
how can achieve model binding these partialview data main viewmodel??
public class questionviewmodel { [display(name = "enter question")] public string question { get; set; } public ilist<questionoptionviewmodel> options { get; set; } public int maxoptions { { return 4; } } public questionviewmodel() { options = new list<questionoptionviewmodel>(); } } public class questionoptionviewmodel { public string option { get; set; } public bool isrightanswer { get; set; } }
in viewm have below,
@using (html.beginrouteform("savequestion", new {}, formmethod.post)){ <div> @html.labelfor(m => m.question) @html.textboxfor(m => m.question) </div> <p>add options question</p> (int = 0; < model.maxoptions; i++) { { html.renderpartial("questionoption", new babbi_test_admin.models.questionoptionviewmodel()); } } <input type="submit" value="save" />
when form submitted have post method as,
[httppost] public void savequestion(questionviewmodel viewmodel) { }
my viewmodel in post has "options" null
this happening cause must binding questionviewmodel action of controller instead, need create viewmodel have questionviewmodel
optionviewmodel
error
[httppost] public void savequestion(questionviewmodel viewmodel) { }
in post method expecting questionviewmodel
how can expect optionviewmodel
another thing renbdering partial view questionoptionviewmodel
contains options , right answer in loop right answer 4 thing incorrect.
sample
public class questionviewmodel { //properties } public class optionviewmodel { //properties } public class questionandoptionviewmodel { // questionviewmodel , optionviewmodel model object //constructors //methods } [httppost] public void savequestion(questionandoptionviewmodel viewmodel) { //your code here; }
Comments
Post a Comment