How do I use a #Html.ActionLink inside a for loop - asp.net-mvc

How can I pass a value of PGId to controller by use a for loop. I want to only use for loop in my case
When I use a for loop It's not work fine:
for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>
#Html.DisplayFor(m => m[i].PGId)
#Html.HiddenFor(m => m[i].PGId)
</td>
<td>
#Html.ActionLink("Edit", "Edit", // pass PGId from here ???)
</td>
</tr>
}
When I use a foreach loop my code work fine
foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.PGId)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.PGId})
</td>
</tr>
}

Use Model[i]:
for (int i = 0; i < Model.Count; i++)
{
<tr>
...
<td>
#Html.ActionLink("Edit", "Edit", new { id = Model[i].PGId })
</td>
</tr>
}

You were on track, it was just the edit part that you didn't know how to complete.
You are working with a list of items, and in your scenario you will need to access each individual item like accessing an array, i.e. with an index field.
for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>
#Html.DisplayFor(m => m[i].PGId)
#Html.HiddenFor(m => m[i].PGId)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = Model[i].PGId })
</td>
</tr>
}
The result HTML code will look something like this:
<tr>
<td>
10001
<input name="[0].PGId" type="hidden" value="10001" />
</td>
<td>
Edit
</td>
</tr>
<tr>
<td>
10002
<input name="[0].PGId" type="hidden" value="10002" />
</td>
<td>
Edit
</td>
</tr>

Related

How to calculate total value from razor view with javascript? MVC project

I got a .Net project using MVC and Razor.
I pass to the view a list containing products, where I have ProdutoValor( product value) with a TextboxFor for QuantidaVenda (Number os itens sold) for each iten. I want to get the value "QuantidadeVenda * ProdutoValor" for each item and show in a textbox the total value. How can I manage to do that with javascript?
I wanto to calculate the total as soon as the user enters the quantity
here is my view :
#for (int i = 0; i < Model.Produtos.Count; i++)
{
<tr>
<td>
#Html.HiddenFor(model => model.Produtos[i].ProdutoId)
#Html.HiddenFor(model => model.Produtos[i].ProdutoDescricao)
#Html.DisplayFor(model => model.Produtos[i].ProdutoNome)#Html.HiddenFor(model => model.Produtos[i].ProdutoNome)
</td>
<td>
#Html.DisplayFor(p => p.Produtos[i].ProdutoValor, new { #class = "form-control", Value = Model.Produtos[i].ProdutoValor }) #Html.HiddenFor(model => model.Produtos[i].ProdutoValor)
</td>
<td>
#Html.DisplayFor(p => p.Produtos[i].ProdutoEstoque, new { #class = "form-control", Value = Model.Produtos[i].ProdutoEstoque })#Html.HiddenFor(model => model.Produtos[i].ProdutoEstoque)
</td>
<td>
#if (Model.Produtos[i].ProdutoEstoque < 1)
{
#Html.TextBoxFor(p => p.Produtos[i].QuantidadeVenda, new { value = 0, disabled = "disabled" })
}
else
{
#Html.TextBoxFor(p => p.Produtos[i].QuantidadeVenda, new { value = 0, #id = "quantidade" })
if (Model.Produtos[i].ProdutoEstoque < Model.Produtos[i].QuantidadeVenda)
{<p>valor invalido</p>}
}
</td>
</tr>
}
[enter image description here][1]
how the list looks like
For Javascript:
Onchange Event for QuantidadeVenda(quantity):
function onchange()
{
var total= document.getElementById("name of total textbox").Value*
document.getElementById("name of quantity textbox").value
document.getElementById("name of total textbox").innerHTML=total
}
Jquery:
on change or LostFocus Event
$("input").change(function(){
var total= $("#name of total textbox").val()*
$("#name of quantity textbox").val()
$("#name of total textbox").val=total
});
Updated Code
HTML:
<table border="1">
<tr>
<td>product</td>
<td>quantity</td>
<td>total</td>
</tr>
<tr>
<td>
10
</td>
<td>
<input type="text" name="txt" value="" onchange="myFunction(this)">
</td>
<td><label>_</label></td>
</tr>
<tr>
<td>
10
</td>
<td>
<input type="text" name="txt" value="" onchange="myFunction(this)">
</td>
<td><label>_</label></td>
</tr>
</table>
Javascript:
function myFunction($this) {
debugger $($this).parent().siblings(":last").find('label').text($($this).parent().siblings().first().text().trim() * $this.value)
}

Create a dynamic amount of columns for each item

I have a template maker which creates Scorecards with a possibility of having 10 sections with up to 10 questions in each. When creating my MVC View I only want to show sections that have questions. I have a column in my DB table that has a count of the sections and also for each section I have a column indicating the amount of questions in that section. I have tried this but the model binding does not like the insertion of dynamic names. I think it could be my syntax. Below is what I tried but it doesn't work:
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
<td>
#Html.DisplayFor(modelItem => item.DepartmentId)
</td>
<td>
#Html.DisplayFor(modelItem => item.TotSections)
</td>
#for (var i = 0; i < item.TotSections; i++ )
{
var section = 1;
var SN = "S" + section + "Name";
var SNUP = "S" + section + "NumUP";
var SNQ = "S" + section + "NumQuest";
<td>
#Html.DisplayFor(modelItem => item[SN])
</td>
<td>
#Html.DisplayFor(modelItem => item[SNUP])
</td>
<td>
#Html.DisplayFor(modelItem => item[SNQ])
</td>
}
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.TemplateId }) |
#Html.ActionLink("Details", "Details", new { id = item.TemplateId }) |
#Html.ActionLink("Delete", "Delete", new { id = item.TemplateId })
</td>
</tr>
}
IS there a way of doing this or do I need to rethink?
Thanks in advance.
Edit:
If I had a Scorcard Template with one section in it the static table row would look like this:
#foreach (var item in Model)
{
<tr>
<!--These are Constant for each item-->
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
<td>
#Html.DisplayFor(modelItem => item.DepartmentId)
</td>
<td>
#Html.DisplayFor(modelItem => item.TotSections)
</td>
<!--BELOW IS WHERE I AM HAVING THE PROBLEM-->
<!--I want to create these columns dynamically,-->
<!--depending on how many sections in column '**TotSections**' above-->
<td>
#Html.DisplayFor(modelItem => item.S1Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.S1NumUP)
</td>
<td>
#Html.DisplayFor(modelItem => item.S1NumQuest)
</td>
<!--Below will be constant for each table row-->
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.TemplateId }) |
#Html.ActionLink("Details", "Details", new { id = item.TemplateId }) |
#Html.ActionLink("Delete", "Delete", new { id = item.TemplateId })
</td>
</tr>
}
if there were two sections:
#foreach (var item in Model)
{
<tr>
<!--These are Constant-->
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
<td>
#Html.DisplayFor(modelItem => item.DepartmentId)
</td>
<td>
#Html.DisplayFor(modelItem => item.TotSections)
</td>
<!--BELOW IS WHERE I AM HAVING THE PROBLEM-->
<!--I want to create these columns dynamically,-->
<!--depending on how many sections in column '**TotSections**' above-->
<td>
#Html.DisplayFor(modelItem => item.S1Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.S1NumUP)
</td>
<td>
#Html.DisplayFor(modelItem => item.S1NumQuest)
</td>
<td>
#Html.DisplayFor(modelItem => item.S2Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.S2NumUP)
</td>
<td>
#Html.DisplayFor(modelItem => item.S2NumQuest)
</td>
<!--Below will be constant-->
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.TemplateId }) |
#Html.ActionLink("Details", "Details", new { id = item.TemplateId }) |
#Html.ActionLink("Delete", "Delete", new { id = item.TemplateId })
</td>
</tr>
}
The end view will look like this:
You can see above under the TotSections column:
Test Template 1 has 1 section. If I display these with a static viewmodel, columns will be displayed when they shouldn't be and columns won't be displayed when they should. As in the above picture it is showing 2 sections worth when it only has one.
Test Template 2 has 6 sections but is only displaying 2 of the six's columns so is missing 4 sections worth of columns.
The code I originally posted was my attempt to get the number of sections from item.TotSections and then create the section columns dynamically.
Hope this is a bit clearer

MVC Razor Code Issue

I'm trying to do something like the following:
<tbody class="searchable">
#foreach (var item in Model)
{
if (item.Account.AccountName != "xyz")
<tr class="danger">
else
<tr>
<td>
#Html.DisplayFor(modelItem => item.LocationName)
</td>
<td>
#Html.DisplayFor(modelItem => item.City)
</td>
</tr>
}
</tbody>
The compiler doesn't like the if statement where I'm trying to create a row tag dynamically. The error I'm getting says the #foreach block is missing a close '}'. My guess is that the 'if' statement is being misinterpreted. Can someone suggest how I can fix this?
You can just avoid the if, and do something like:
<tr class='#(item.Account.AccountName != "xyz" ? "danger" : "")'>
<td>
#Html.DisplayFor(modelItem => item.LocationName)
</td>
<td>
#Html.DisplayFor(modelItem => item.City)
</td>
</tr>

How to pass a list of objects to a [HTTPPost]controller parameter, in ASP.Net MVC?

i have the next view:
#model IEnumerable<L5ERP.Model.BLL.BusinessObjects.MTR_MonthlyTransfer>
#using (Html.BeginForm("ExpenseMonthlyTransferProcessing", "BudgetTransfer", Model.ToList())){
<table class ="divTable">
<tr>
<th>Transferir</th>
<th>
Clave
</th>
<th>
Monto
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.CheckBoxFor(x => item.MTR_Bool, new { #class = "checkMTR", #checked = "checked" })
</td>
<td>
#Html.TextBoxFor(x => item.MTR_Key, new {#class = "longInput" })
</td>
<td>
#String.Format("{0:F}", item.MTR_Amount)
</td>
</tr>
}
</table>
}
and my controller like this
[HttpPost]
public ActionResult ExpenseMonthlyTransferProcessing(List<MTR_MonthlyTransfer> lstMtr)
{ return View(lstMTR); }
But when i do the post my list is null, how can i send my list through the submit button ?
You should change the #model to an array (L5ERP.Model.BLL.BusinessObjects.MTR_MonthlyTransfer[]) or something else that implements IList<>:
#model L5ERP.Model.BLL.BusinessObjects.MTR_MonthlyTransfer[]
#for (var i = 0; i < Model.Length; i ++) {
<tr>
<td>
#Html.CheckBoxFor(x => Model[i].MTR_Bool, new { #class = "checkMTR", #checked = "checked" })
</td>
<td>
#Html.TextBoxFor(x => Model[i].MTR_Key, new {#class = "longInput" })
</td>
<td>
#String.Format("{0:F}", item.MTR_Amount)
</td>
</tr>
receive a FormCollection and parse the items in it manually
Use F12 to check the post in your navigator to see if it are sending the content you expected.

Html.HiddenFor causing error

When I try to add #Html.HiddenFor(#Model.ID) to my code, I get the following error when access the page:
Compiler Error Message: CS0411: The type arguments for method 'System.Web.Mvc.Html.InputExtensions.HiddenFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
I tried reading MSDN, but the documentation is awful (they don't provide a single code example in the documentation for this method.
Here is my View:
#model CustomerService.Entity.Order
#using CustomerService.Entity
#{
ViewBag.Title = "OrderDetails";
}
<h2>
OrderDetails</h2>
#using (Html.BeginForm("HandleSubmit", "Home", FormMethod.Post))
{
<table border="1">
<tr>
<td>
<b>Order #</b>
</td>
<td>
#Model.ID
</td>
</tr>
<tr>
<td>
<b>Description</b>
</td>
<td>
#Model.Description
</td>
</tr>
<tr>
<td>
<b>Salesperson Name</b>
</td>
<td>
#Model.SalespersonName
</td>
</tr>
</table>
<h3>
Line Items</h3>
<input id="btnAddLineItem" type="submit" name="AddLineItem" value="AddLineItem" />
#Html.HiddenFor(#Model.ID)
<table border="1">
<tr>
<td>
<b>Line Item ID</b>
</td>
<td>
<b>Description</b>
</td>
</tr>
#for (int i = 0; i < #Model.LineItems.Count; ++i)
{
<tr>
<td>
#Model.LineItems[i].ID
</td>
<td>
#Model.LineItems[i].Description
</td>
</tr>
}</table>
}
HiddenFor takes an expression.
#Html.HiddenFor( model => model.ID )
HiddenFor method should get an Expression as a parameter not a value:
#Html.HiddenFor(m => m.ID)
Instead of: #Html.HiddenFor(#Model.ID)
Method signature:
HiddenFor<TModel, TProperty>(HtmlHelper<TModel>,
Expression<Func<TModel, TProperty>>)
In plain text you should give an Expression that gets an "instance" of the type of your model(in this case CustomerService.Entity.Order) and returns the desired property(in this case ID)
MSDN

Resources