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="[email protected]" value="1" id="optionAS_1" onclick="disableAs(this,@item.QuestionId,1)"/>                                                
                </label>
            </td>
            <td align="center">
                <label>
                    <input type="radio" name="[email protected]" 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?

2
  • Can you give some more information about your View ? Commented Aug 31, 2013 at 14:33
  • I have update the question. Now it has more info about view. I want all values of radio button to my controller by IList<Question>. Commented Aug 31, 2013 at 18:34

1 Answer 1

1

You should use html helpers to bind properties of your model, your code might be as follows:

@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>
}

and so for. HiddenFor helper is needed to create hidden input to send Id value to server to give you ability to identify you object. Take a look into Html Helpers in MVC and you will have your model back to server when form is submitted.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.