c# - MVC post form return null -
this question has answer here:
i'm trying make dynamic form tests. problem when submit from, in post method in controller list gq null, im expecting filled data filled in form. can guess have @html.editorformodel() , data binding, dont understand how works. idea? thank you
model:
public class generatedquestions { public int generatedquestionsid { get; set; } public string questiontext { get; set; } public list<studentanswer> questionanswers { get; set; } public int studenttestsid { get; set; } public virtual studenttest studenttests { get; set; } }
view:
@model ienumerable<project.models.generatedquestions> @{ viewbag.title = "starttest"; } @using (html.beginform()) { @html.editorformodel() foreach (var question in model) { <br /> <h1>@question.questiontext</h1> foreach (var answer in question.questionanswers) { @html.checkboxfor(x => answer.waschecked) @html.displayfor(x => answer.answertext) @html.hiddenfor(x => answer.generatedquestionsid) <br /> } } <input type="submit" value="submit" /> }
controller:
public actionresult starttest(int id) { list<generatedquestions> generatedquestions = db.studenttests.find(id).generatedquetions.tolist(); return view(generatedquestions); } [httppost] public actionresult starttest(list<generatedquestions> gq) { //stuff return redirecttoaction("index"); }
as seen on here:
asp.net mvc 4 - loop posts model collection properties foreach not
mvc helpers hate foreach loops. go for loop , should work fine.
write both loop , inspect them using web developer tools see difference in 'id' , 'name' attribute.
Comments
Post a Comment