ASP.NET MVC Update fields of data Table at once - asp.net

I have this view and I need to update the table Task upon the chosen values at Value Column View: EstimateValue. The view accepts #model IEnumerable then iterate the list of tasks and display them in Table.
The View
#model IEnumerable<Task>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<table class="table table-striped table-hover table-bordered">
<thead>
<tr class="info">
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Description)
</th>
<th>
#Html.DisplayNameFor(model => model.Value)
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
#Html.HiddenFor(modelItem => item.ID)
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
<td>
#Html.EditorFor(modelItem => item.Value, new { #class = "form-control" })
#Html.ValidationMessageFor(modelItem => item.Value, "", new { #class = "text-danger" })
</td>
</tr>
}
</tbody>
</table>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
The Controller has the post method EstimateValue
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EstimateValue(IEnumerable<Task> TaskList)
{
if (ModelState.IsValid)
{
foreach (var task in TaskList)
{
db.Entry(task).State = EntityState.Modified;
db.SaveChanges();
}
return RedirectToAction("Index");
}
//1. Rebound List to view
var TaskList = db.Tasks.ToList();
//2, return model to view
return View(TaskList);
}
When running the application the TaskList is null and I could not update the table Task. I have used JSON to send the data from the view as an array but still the values of TaskList are null

Change your IEnumerable to List because IEnumerable does not allow indexing. And instead of #foreach loop, use #for loop. So your View should be like this:
#model List<Task>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<table class="table table-striped table-hover table-bordered">
<thead>
<tr class="info">
<th>
#Html.DisplayNameFor(model => Model[0].Name)
</th>
<th>
#Html.DisplayNameFor(model => Model[0].Description)
</th>
<th>
#Html.DisplayNameFor(model => Model[0].Value)
</th>
</tr>
</thead>
<tbody>
#for(int i=0;i<Model.Count;i++)
{
<tr>
#Html.HiddenFor(modelItem => modelItem[i].ID)
<td>
#Html.DisplayFor(modelItem => modelItem[i].Name)
</td>
<td>
#Html.DisplayFor(modelItem => modelItem[i].Description)
</td>
<td>
#Html.EditorFor(modelItem => modelItem[i].Value, new { #class = "form-control" })
#Html.ValidationMessageFor(modelItem => modelItem[i].Value, "", new { #class = "text-danger" })
</td>
</tr>
}
</tbody>
</table>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
Then change your Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EstimateValue(List<Task> TaskList)
{
if (ModelState.IsValid)
{
for(int i=0;i<TaskList.Count;i++)
{
db.Entry(TaskList[i]).State = EntityState.Modified;
db.SaveChanges();
}
return RedirectToAction("Index");
}
//1. Rebound List to view
var TaskList = db.Tasks.ToList();
//2, return model to view
return View(TaskList);
}
If this solves your problem, accept the answer.

Related

How to call drop down list by ajax call request when selected value changed?

I work on web app mvc .NET framework I face issue i can't call function GetSelectedDropDownChanged when drop down selected changed .
I put break point to function GetSelectedDropDownChanged on controller WorkforceRequests
but it not hit or catched so what is issue
so what I try is :
<th>
<select class="form-control" id="statusselect" name="statusselectName">
<option value="1">Pending Request</option>
<option value="2">All requests </option>
</select>
</th>
when select from drop down I expect to call it ajax request call to function WorkforceRequests controller WorkforceRequests :
<script>
$(function () {
$(document)
.on('change', '#statusselect', function () {
var valueofDropDown = $(this).val();
var url = '/WorkforceRequests/GetSelectedDropDownChanged';
var dataToSend = { selectedValue: valueofDropDown }
$.ajax({
url: url,
data: dataToSend,
type: 'GET',
success: function (dataReceived) {
//update control on View
var receivedValue = dataReceived.myResult;
$('YourControlIDToUpdate').val(receivedValue);
}
})
});
};
</script>
function i call from ajax request as below :
public class WorkforceRequestsController : Controller
{
[HttpGet]
public JsonResult GetSelectedDropDownChanged(string selectedValue)
{
List<WorkforceRequest> workforceRequest = new List<WorkforceRequest>();
if (selectedValue == "1")
{
workforceRequest = Workforce.GetPendingOnPalnningSection();
}
else
workforceRequest = Workforce.GetAllRequestsData();
return Json(new { myResult = workforceRequest }, JsonRequestBehavior.AllowGet);
}
}
data will returned as list from ajax call from function GetSelectedDropDownChanged ON table id dtbl
<table id="dtbl" class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th></th>
<th>
#Html.DisplayNameFor(model => model.WorkforceRequestID)
</th>
<th>
#Html.DisplayNameFor(model => model.DepartmentCode)
</th>
<th>
#Html.DisplayNameFor(model => model.Section)
</th>
<th>
#Html.DisplayNameFor(model => model.RequiredPosition)
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.WorkforceRequestID)
</td>
<td>
#Html.DisplayFor(modelItem => item.DepartmentCode)
</td>
<td>
#Html.DisplayFor(modelItem => item.Section)
</td>
<td>
#Html.DisplayFor(modelItem => item.RequiredPosition)
</td>
</tr>
}
</tbody>
</table>
Last Updated
I change my code inside view.cshtml to :
#model IEnumerable<HR.WorkforceRequisition.Models.WorkforceRequest>
#{
ViewBag.Title = "Pending Requests";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Pending Requests</h2>
<hr />
#if (!string.IsNullOrWhiteSpace(ViewBag.msg))
{
<div class="alert alert-success">
#ViewBag.msg
</div>
}
#if (!string.IsNullOrWhiteSpace(ViewBag.errorMsg))
{
<div class="alert alert-danger">
#ViewBag.errorMsg
</div>
}
#if (Session[BLL.Common.SessionKeys.RoleCode].ToString() == "REC" || Session[BLL.Common.SessionKeys.RoleCode].ToString() == "PLNG")
{
<th>
<select class="form-control" id="statusselect" >
#*<option>Select Status</option>*#
<option value="1">Pending Request</option>
<option value="2">All requests </option>
</select>
</th>
}
<table id="dtbl" class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th></th>
<th>
#Html.DisplayNameFor(model => model.WorkforceRequestID)
</th>
<th>
#Html.DisplayNameFor(model => model.DepartmentCode)
</th>
<th>
#Html.DisplayNameFor(model => model.Section)
</th>
<th>
#Html.DisplayNameFor(model => model.RequiredPosition)
</th>
<th>
#Html.DisplayNameFor(model => model.JobTitle)
</th>
<th>
#Html.DisplayNameFor(model => model.ReportingTo)
</th>
<th>
#Html.DisplayNameFor(model => model.NationalityCategory)
</th>
<th>
#Html.DisplayNameFor(model => model.StatusRemark)
</th>
<th>
Requestor
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.ActionLink("Details", "Details", new { id = item.WorkforceRequestID })
</td>
<td>
#Html.DisplayFor(modelItem => item.WorkforceRequestID)
</td>
<td>
#Html.DisplayFor(modelItem => item.DepartmentCode)
</td>
<td>
#Html.DisplayFor(modelItem => item.Section)
</td>
<td>
#Html.DisplayFor(modelItem => item.RequiredPosition)
</td>
<td>
#Html.DisplayFor(modelItem => item.JobTitle)
</td>
<td>
#Html.DisplayFor(modelItem => item.ReportingTo)
</td>
<td>
#Html.DisplayFor(modelItem => item.NationalityCategory)
</td>
<td>
#Html.DisplayFor(modelItem => item.StatusRemark)
</td>
<td>
#Html.DisplayFor(modelItem => item.CreatedByName)
</td>
</tr>
}
</tbody>
</table>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
#section scripts
{
<script>
$(document).ready(function () {
$("#statusselect").change(function () {
var valueofDropDown = $("#statusselect").val();
console.log(valueofDropDown);
var url = #Url.Action("GetSelectedDropDownChanged", "WorkforceRequests");
var dataToSend = { selectedValue: valueofDropDown }
$.ajax({
url: url,
data: dataToSend,
type: 'GET',
success: function (dataReceived) {
//update control on View
var receivedValue = dataReceived.myResult;
$('YourControlIDToUpdate').val(receivedValue);
}
})
});
}
</script>
}
Issue still not solved yet
#ahmed abdelaziz please check below code
you write a url before after get the value
var valueofDropDown = $(this).val();
var url = '/WorkforceRequests/GetSelectedDropDownChanged?selectedValue='+valueofDropDown;
have you check this one because I am not able to show in your code
#section scripts {
//stuff
}
_Layout.cshtml
#RenderSection("scripts", required: false)

Stored Procedure Error-'Procedure or function 'requisition_sp_setstatus0' expects parameter '#ReqNumber', which was not supplied

When I click on the approve I'm getting this error : 'Procedure or function 'requisition_sp_setstatus0' expects parameter '#ReqNumber', which was not supplied.' once I remove this code of lines
#*<tr>
<td>#Html.CheckBoxFor(m => m[i].postTrnx, new { #class = "checkGroup1" })</td>
<td class="label">
#Html.DisplayFor(m => m[i].reqNumber)
#Html.DisplayFor(m => m[i].reqDate)
</td>
</tr>
but I don't want to be in that format but showcase in the table. Why isn't the function picking up the requisition number from the table but picking it up from display. I have to select the display part inorder for it to be approve.
public void SetRequisitionStatus0(List<string> docNumbers)
{
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand();
command.CommandText = "requisition_sp_setstatus0";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("#ReqNumber", SqlDbType.VarChar);
command.Parameters.Add("#approve_date", SqlDbType.DateTime).Value = DateTime.Now;
using (command.Connection = connection)
{
try
{
connection.Open();
foreach (var item in docNumbers)
{
command.Parameters["#ReqNumber"].Value = item;
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
connection.Close();
}
}
return;
}
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div>
<hr />
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
#*<input type="submit" value="Issue" name="Issue" class="btn btn-default" />*#
#*<input type="submit" value="Search" name="Search" class="btn btn-default" />*#
</div>
</div>
<table id="data">
<thead>
<tr>
<th class="col-lg-1">#Html.CheckBox("TheOneCheckBoxToRuleThemAll")Select All</th>
<th class="col-lg-1 ">Date</th>
<th class="col-lg-1 ">Requisition Number</th>
<th class="col-lg-1 ">Expense Account</th>
<th class="col-lg-1">Requestor</th>
<th class="col-lg-1">Department</th>
<th class="col-lg-1">LoggedinAs</th>
<th class="col-lg-1 ">Item Number</th>
<th class="col-lg-1 ">Description</th>
<th class="col-sm-1">Quantity</th>
<th class="col-sm-1 ">UOM</th>
</tr>
<tbody>
#for (int i = 0; i < Model.Count; i++)
{
#Html.HiddenFor(m => m[i].reqNumber)
#Html.HiddenFor(m => m[i].department)
#Html.HiddenFor(m => m[i].department)
#*<tr>
<td>#Html.CheckBoxFor(m => m[i].postTrnx, new { #class = "checkGroup1" })</td>
<td class="label">
#Html.DisplayFor(m => m[i].reqNumber)
#Html.DisplayFor(m => m[i].reqDate)
</td>
</tr>*#
foreach (var item in Model[i].items)
{
#Html.HiddenFor(m => item.description)
#Html.HiddenFor(m => item.expense_account)
#Html.HiddenFor(m => item.itemNumber)
<tr>
<td>#Html.CheckBoxFor(m => m[i].postTrnx, new { #class = "checkGroup1" })</td>
<td class="col-lg-1 tabledata" >#item.requisition.reqDate</td>
<td class="col-lg-1 tabledata" >#item.requisition.reqNumber</td>
<td class="col-lg-1 tabledata">#item.expense_account.account_desc</td>
<td class="col-lg-1 tabledata">#item.employeeDetails.employeeNum</td>
<td class="col-lg-1 tabledata">#item.employeeDetails.department</td>
<td class="col-lg-1 tabledata">#item.employeeDetails.LoggedInUserName</td>
<td class="col-lg-1 tabledata">#item.itemNumber</td>
<td class="col-lg-1 tabledata">#item.description</td>
<td class="col-sm-1 tabledata">#item.quantity</td>
<td class="col-sm-1 tabledata">#item.selecteduomtext </td>
#*<td>#Html.ActionLink("Edit", "Edit", new { id = #item.lineNum, name = Model[i].reqNumber })</td>*#
</tr>
}
}
</tbody>
</table>
<br /><br /><br />
<div>
<input type="submit" value="Approve" name="Approve" class="btn btn-default" onclick="return confirm('Click OK to continue or Cancel to abort');" />
</div>
}

post method in asp.net mvc4 goes to other page

I am working with asp.net mvc4.
I have a controller:
public class ExtendedProfileController : ProfileController
{
protected override void RegisterSystemRoutes(SanaRouteCollection routes)
{
routes.MapSystemPageRoute("Profile", "BalieNr", "BalieNr", "Profile/BalieNr");
base.RegisterSystemRoutes(routes);
}
[HttpGet]
public ActionResult Hallo()
{
return View();
}
[HttpGet]
public ActionResult BalieNr()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult BalieNr(IEntityListLoadOptions options, string accountID, ExtendedSalesAgentInfoModel model /*string accountId, ShopAccountType accountType,Customer.ICustomerListLoadOptions hallo*/)
{
var salesAgent = CommerceFramework.ShopAccounts.GetShopAccounts(options);
foreach (var item in salesAgent)
{
if (item.ShopAccountType == ShopAccountType.SalesAgent)
{
if (item.ReferenceId.Contains("DB"))
{
Console.WriteLine("true");
}
var customer = CommerceFrameworkBase.SalesPersons.GetSalesPerson(accountID);
Console.WriteLine(accountID);
}
else
Console.WriteLine("false");
}
Console.WriteLine(salesAgent);
return View();
}
}
and the method BalieNr is the main method.
So I also have a view, like this:
#{
Layout = LayoutPaths.General;
}
#model Sana.Commerce.Customization.Account.ExtendedSalesAgentInfoModel
<div class="semicolumn">
#*<div class="text">#Sana.RichText("Login_IntroductionText", makeImagesResponsive: true)</div>*#
<div class="form-holder">
#using (Html.BeginForm("BalieNr", "ExtendedProfileController", FormMethod.Post))
{
#Html.AntiForgeryToken()
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.Baliecode)
</th>
<th></th>
</tr>
<tr>
<td>
#Html.TextBoxFor(modelItem => modelItem.Baliecode)
</td>
</tr>
</table>
<div class="form-row">
#*#Sana.SubmitButton("Login", "btn btn-medium btn-login")*#
<input type="submit" value="Login" />
</div>
}
</div>
<div>
</div>
</div>
So I define a action method and a controller name.
But if I do a post then it doesnt goes to my post action method in the controller - BalieNr. But it goes to this link:
http://localhost:5923/sitemap.xml
and also if I put a breakpoint on the post method BalieNr. it doesnt hit that method.
So how to solve it so that it hits the post method BalieNr?
Thank you
if I do this:
#using (Html.BeginForm("BalieNr", "ExtendedProfile", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.AntiForgeryToken()
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.Baliecode)
</th>
<th></th>
</tr>
<tr>
<td>
#Html.TextBoxFor(modelItem => modelItem.Baliecode)
</td>
</tr>
</table>
<div class="form-row">
#*#Sana.SubmitButton("Login", "btn btn-medium btn-login")*#
<input type="submit" value="Login" />
</div>
}
still doesnt hit the correct method.
I have done it like this:
#{
Layout = LayoutPaths.General;
}
#model Sana.Commerce.Customization.Account.ExtendedSalesAgentInfoModel
#*#Sana.RichText("Login_IntroductionText", makeImagesResponsive: true)*#
#using (Html.BeginForm(htmlAttributes: new { #class = "form" }))
{
#Html.AntiForgeryToken()
#Html.DisplayNameFor(model => model.Baliecode)
#Html.TextBoxFor(modelItem => modelItem.Baliecode)
#*#Sana.SubmitButton("Login", "btn btn-medium btn-login")*#
}
and now it hits the post method :)
This works:
#{
Layout = LayoutPaths.General;
}
#model Sana.Commerce.Customization.Account.ExtendedSalesAgentInfoModel
<div class="semicolumn">
#*<div class="text">#Sana.RichText("Login_IntroductionText", makeImagesResponsive: true)</div>*#
<div class="form-holder">
#using (Html.BeginForm(htmlAttributes: new { #class = "form" }))
{
#Html.AntiForgeryToken()
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.Baliecode)
</th>
<th></th>
</tr>
<tr>
<td>
#Html.TextBoxFor(modelItem => modelItem.Baliecode)
</td>
</tr>
</table>
<div class="form-row">
#*#Sana.SubmitButton("Login", "btn btn-medium btn-login")*#
<input type="submit" value="Login" />
</div>
}
</div>
<div>
</div>
</div>

Html rendering order for asp mvc

How come my form loads like this?
Here is my code :
#using MvcApplication6.Models;
#model List<Employee>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
Create Entry
</p>
<p>#Html.ActionLink("Check departments", "Index", "Department")</p>
<p>Check summary</p>
<table border="1">
<tr>
<th>
#Html.DisplayNameFor(model => model[0].id)
</th>
<th>
#Html.LabelFor(model => model[0].firstname)
</th>
<th>
#Html.LabelFor(model => model[0].lastname)
</th>
<th>
#Html.DisplayNameFor(model => model[0].gender)
</th>
<th>
#Html.DisplayNameFor(model => model[0].department)
</th>
<th>Action</th>
</tr>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
foreach (Employee mod in Model)
{
<tr>
<td>
<input type="hidden" value="#mod.id" name="id" id="id" />
#Html.DisplayFor(modelItem => mod.id)
</td>
<td>
#Html.DisplayFor(modelItem => mod.firstname)
</td>
<td>
#Html.DisplayFor(modelItem => mod.lastname)
</td>
<td>
#Html.DisplayFor(modelItem => mod.gender)
</td>
<td>
#Html.DisplayFor(modelItem => mod.department)
</td>
<td>
<button onclick="location.href='#Url.Action("Edit", new { id = mod.id })';return false;">Edit</button>
#Html.CheckBoxFor(modelItem => mod.selected)
</td>
</tr>
}
<br />
<input type="submit" value="submit" />
}
</table>
Here are my concerns:
Notice that i put my submit button after the foreach loop but how come it was rendered first before the actual boxes?
I've been trying to put it on the bottom but i am having some problems.
What's the ordering for render?

Why the "FirstOrDefault()" is not required in the <th> rasor code?

I'm trying to build a partial view which can add a record at the top and show the list of added items.
I have the following code. Why it's not necessary to add FirstOrDefault() in the <th> part of the html table while I had to do it in the "Add" html form? It seems FirstOrDefault() is always required since the model is IEnurable<>. However, check all the scaffold Index.cshtml files, you will find there is no FirstOrDefault() in the table head.
Is it a better way to implement one edit page for both adding and deleting actions?
#model IEnumerable<Models.MyModel>
#using (Html.BeginForm("AddItem", "MyAction", FormMethod.Post))
{
<div class="editor-label">
#Html.LabelFor(model => model.FirstOrDefault().Col1)
</div>
<div class="editor-field">
#Html.EditorFor(m => m.FirstOrDefault().Col1)
#Html.ValidationMessageFor(m => m.FirstOrDefault().Col1)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.FirstOrDefault().Col2)
</div>
<div class="editor-field">
#Html.EditorFor(m => m.FirstOrDefault().Col2)
#Html.ValidationMessageFor(m => m.FirstOrDefault().Col2)
</div>
<input type="submit" name="Submit" id="Submit" value="Add" />
}
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.Col1)
</th>
<th>
#Html.DisplayNameFor(model => model.Col2)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Col1)
</td>
<td>
#Html.DisplayFor(modelItem => item.Col2)
</td>
<td>
#Html.ActionLink("Delete", "Delete", new { item.Col1, item.Col2})
</td>
</tr>
}
</table>
Update:
I changed it to #Html.EditorFor(m => (new Model.MyModel()).Col1) and this will not carry the value of the first row in the inserting inputs. This should be one of the solution.
You have to add FirstOrDefault because your model is IEnumerable.
So in order to get an instance you have to get one element from the IEnumerable thus, FirstOrDefault
This presents a problem though, for if the model is null, the page will throw a runtime error when trying to access that property Col1.
In the table, you are iterating over the items in the model therefore you won't need to do FirstOrDefault
I would recommend creating a model that has a property that is the instance that you're looking to bind to.
Example:
public class MyViewModel {
public MyModel MyModel { get; set; }
public IEnumerable<MyModel> MyModels { get;set; }
}
Then:
#model MyNameSpace.MyViewModel
<div class="editor-field">
#Html.EditorFor(m => m.MyModel.Col1)
#Html.ValidationMessageFor(m => m.MyModel.Col1)
</div>
You can Avoid using IEnumerable model in your View.
Model would be something like below.
public class MyModel
{
public string Col1 { get; set; }
public string Col2 { get; set; }
public string Email { get; set; }
public bool IsAdministrator { get; set; }
}
public class MyLstModel:MyModel
{
public IEnumerable<MyModel> lstMyModel { get; set; }
}
View:
#model Models.MyLstModel
#using (Html.BeginForm("AddItem", "MyAction", FormMethod.Post))
{
<div class="editor-label">
#Html.LabelFor(model => model.Col1)
</div>
<div class="editor-field">
#Html.EditorFor(m => m.Col1)
#Html.ValidationMessageFor(m => m.Col1)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Col2)
</div>
<div class="editor-field">
#Html.EditorFor(m => m.Col2)
#Html.ValidationMessageFor(m => m.Col2)
</div>
<input type="submit" name="Submit" id="Submit" value="Add" />
}
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.Email)
</th>
<th>
#Html.DisplayNameFor(model => model.IsAdministrator)
</th>
<th></th>
</tr>
#foreach (var item in Model.lstMyModel) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Col1)
</td>
<td>
#Html.DisplayFor(modelItem => item.Col2)
</td>
<td>
#Html.ActionLink("Delete", "Delete", new { item.Col1, item.Col2})
</td>
</tr>
}
</table>

Resources