It may duplicate question, i have searched all over but couldn't satisfied, so i am posting here question.
I have object as (generated from entity framework),
public partial class Usp_Sel_NotEnteredStringResources_Result
{
public string name { get; set; }
public Nullable<int> Value { get; set; }
public int StringResourceId { get; set; }
public Nullable<int> LanguageId { get; set; }
}
and view i have created as,
@{
ViewBag.Title = "Resource Entry Languagewise";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>ResourceEntry</h2>
@using (Html.BeginForm("ResourceEntry", "LangResource", FormMethod.Post))
{
<fieldset>
<table>
@for (int i = 0; i < Model.ToList().Count; i++)
{
<tr>
<td>@Html.DisplayFor(m => Model.ElementAt(i).name)</td>
<td>@Html.TextBoxFor(m => Model.ElementAt(i).Value)</td>
@Html.HiddenFor(m => Model.ElementAt(i).LanguageId)
@Html.HiddenFor(m => Model.ElementAt(i).name)
@Html.HiddenFor(m => Model.ElementAt(i).StringResourceId)
</tr>
}
</table>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
And Controller as,
[HttpPost]
public ActionResult ResourceEntry(List<Usp_Sel_NotEnteredStringResources_Result> list)
{
// here getting the null value for list
// ??????
return View(list);
}
After submitting the form controller gets null value for list, what is wrong with code??
You cannot use
.ElementAt()
. If you inspect the html your generating you will see that thename
attribute has no relationship to your model.You model needs to implemet
IList<Usp_Sel_NotEnteredStringResources_Result>
and use afor
loopAlternative if the model is
IEnumerable<Usp_Sel_NotEnteredStringResources_Result>
, you can use anEditorTemplate
(refer [Post an HTML Table to ADO.NET DataTable for more details on how thename
attributes must match your model property names, and the use of anEditorTemplate
)