jeudi 14 avril 2016

Get Form data from the input fields Which names are generated by model dynamically into the controller

I have a controller as given below

[HttpPost]
    public ActionResult LoginResult()
    {
        //want get the data here
        return View();
    }

and I have a view in which I am rendering a view by using a model as given below

@model IEnumerable<My.Models.SurveyData>
@{
    ViewBag.Title = "Index";
}

<h2>Survey Form</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    @using (Html.BeginForm("LoginResult", "My"))
    {        
        foreach (var item in Model)
        {
            <tr>
                <h4> @Html.DisplayFor(modelItem => item.Question)</h4>
                <br />

            @if ((int)item.InputType == 1)
            {

                <input type="text" id="@item.Id" name="@item.Id" value="" />
            }
            else if ((int)item.InputType == 2)
            {
                <textarea rows="4" cols="50" id="@item.Id" name="@item.Id"></textarea>
            }
            else if (item.InputType.ToString().Trim().ToLower() == "checkbox")
            {
                for (int i = 0; i < @item.NumberOfChoice; i++)
                {
                    <input type=@item.InputType.ToString().Trim().ToLower() name="@item.AdminAnswers.ElementAt(i).Answer"></input>@item.AdminAnswers.ElementAt(i).Answer
                    <br />
                }
            }

        }
    <input type="submit" value="Login" />
}

Now I want to get All the input fields value on form submission into the LoginResult Method which is in Controller named "My",but all the input fields name will come dynamically. so how can I get this values in the method.

Aucun commentaire:

Enregistrer un commentaire