با سلام
من در پروژه ام لازم دارم که لیستی از اطلاعات (IEnumerable) را در یک جدول به کاربر نمایش دهم و کاربر قادر باشد بتواند همان اطلاعات را ویرایش کند و لیستی به کنترلر پاس شود.
لطفاً راهمنایی ام کنید.
با تشکر
مشکل کجاست ؟
public ActionResult Index() { CompanyEntities DbCompany = new CompanyEntities(); var data = from item in DbCompany.Employees where item.Company == "TCS" orderby item.ID select item; return View(data.ToList()); }
کد View
@using (Html.BeginForm("Index", "Sample", FormMethod.Post)) { <table class="table"> @for (int i = 0; i < Model.Count; i++) { <tr> <td> @Html.TextBox("employees[" + @i + "].ID", Model[i].ID, new {@readonly="readonly"}) </td> <td> @Html.TextBox("employees[" + @i + "].Name", Model[i].Name, new { @readonly = "readonly" }) </td> <td> @Html.TextBox("employees[" + @i + "].Gender", Model[i].Gender, new { @readonly = "readonly" }) </td> <td> @Html.TextBox("employees[" + @i + "].Company", Model[i].Company, new { @readonly = "readonly" }) </td> </tr> } <tr> <td colspan="4"> <input type="submit" value="Submit" /> </td> </tr> </table> }
اکشن Post
[HttpPost] public ActionResult Index(List<Employee> employees) { CompanyEntities DbCompany = new CompanyEntities(); foreach (Employee Emp in employees) { Employee Existed_Emp = DbCompany.Employees.Find(Emp.ID); Existed_Emp.Name = Emp.Name; Existed_Emp.Gender = Emp.Gender; Existed_Emp.Company = Emp.Company; } DbCompany.SaveChanges(); return View(); }
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)