0

Can anyone give me an example of saving all html grid data in one time. I have a view like this.

@model IList<SURVEY.Models.Question>
@using (Html.BeginForm("Index", "Survey", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-3" }))
{               
    @foreach(var item in Model)
    {
        <tr>
            <td>@item.Ans1</td>
            <td align="center">
                <label>
                    <input type="radio" name="optionAS_@item.QuestionId" value="1" id="optionAS_1" onclick="disableAs(this,@item.QuestionId,1)"/>                                                
                </label>
            </td>
            <td align="center">
                <label>
                    <input type="radio" name="optionAS_@item.QuestionId" value="2" id="optionAS_1" onclick="disableAs(this,@item.QuestionId,2)"/>
                </label>
            </td>
        </tr>
    }
}

I am getting null value for these controls in controller post.

[HttpPost]
public ActionResult Index(IList<Question> ques)
{         
    return View();
}

I am getting ques is null here. Can anyone tell me how can I resolve this?

4

1 回答 1

1

您应该使用 html 助手来绑定模型的属性,您的代码可能如下所示:

@for(var i = 0; i < Model.Count; i++)
{
  <tr>
     <td>@Html.HiddenFor(_ => Model[i].Id)
         Model[i].Ans1
     </td>
     <td align="center">
       <label>
         @Html.RadioButtonFor(_ => Model[i].Name)
       </label>
     </td>
     ...
  </tr>
}

等等。需要 HiddenFor 帮助器来创建隐藏输入以将 Id 值发送到服务器,从而使您能够识别您的对象。看看MVC 中的 Html Helpers,当表单提交时,你将把你的模型返回到服务器。

于 2013-08-31T18:54:34.150 回答