i am increment i in this code which i am not able to parse. what is wrong with i in the code below?
#{
int i=0;
}
#foreach (var item in Model.PortServiceTariffIncluded)
{
<tr id="#("sp" + item.ServiceId)">
<td>#item.ServiceName <input type="hidden" name="QuotationDetailList[i].ServiceId" value="#item.ServiceId" /></td>
<td>#item.ServiceCriteria</td>
<td>#item.ItemBasis</td>
<td>#Html.DropDownListFor(model => model.Currency, ViewBag.CurrencyDd as SelectList) <input type ="text" id="Amount#i" name="QuotationDetailList[i].Amount" /></td>
<td><input type="hidden" value="#item.ServiceId" class="serviceslist" name="serviceslist" /><input type ="button" id="#item.ServiceId" name="btnremove" value="Remove" class="clsremove" /></td>
</tr>
i = i + 1;
}
Use the following, instead:
#foreach (var item in Model.PortServiceTariffIncluded.Select((value, i) => new { i, value })
{
<tr id="#("sp" + item.value.ServiceId)">
<td>#item.ServiceName <input type="hidden" name="QuotationDetailList[item.i].ServiceId" value="#item.value.ServiceId" /></td>
<td>#item.value.ServiceCriteria</td>
<td>#item.value.ItemBasis</td>
<td>#Html.DropDownListFor(model => model.Currency, ViewBag.CurrencyDd as SelectList) <input type ="text" id="Amount#item.i" name="QuotationDetailList[item.i].Amount" /></td>
<td><input type="hidden" value="#item.value.ServiceId" class="serviceslist" name="serviceslist" /><input type ="button" id="#item.value.ServiceId" name="btnremove" value="Remove" class="clsremove" /></td>
</tr>
}
Basically, this causes your item variable to be an object consisting of an iterator (item.i) and the actual item (item.value).
First, I would set a variable to contain your QuotationDetailList :
#{ var QuotationDetailList = Model.QuotationDetailList;}
Then, use a for here instead of a foreach :
#for (var ListIndex = 0; ListIndex < Model.PortServiceTariffIncluded.Count(); ListIndex++)
Now you can reference items using your variable and the index :
<td>#QuotationDetailList[ListIndex].ServiceName <input type="hidden" name="#QuotationDetailList[ListIndex].ServiceId" value="#QuotationDetailList[ListIndex].ServiceId" /></td>
Related
I am new to knockout.js. I have a Y/N value from my model that I want to bind to a checkbox.
This is my view:
<tbody id="tblMultiEdit" data-bind="foreach: UUTs">
<tr>
<td data-bind="text: SerialNumber"></td>
<td><input type="checkbox" data-bind="ReqDowngrade" /></td>
<td><input type="checkbox" data-bind="ACTSupported"/></td>
<td><input type="checkbox" data-bind="ProdModeOff"/></td>
</tr>
</tbody>
Knockout code:
function ViewModel(UUTs)
{
var self = this;
self.UUTs = UUTs;
};
var viewModel = new ViewModel(#Html.HtmlConvertToJson(Model));
ko.applyBindings(viewModel);
So far I can bind the text with no problem, but checkboxes are in blank.
I was able to resolve this by adding a ternary operation.
<td><input type="checkbox" data-bind="checked: (ReqDowngrade == 'Y' ? 1 : 0)" /></td>
If anybody knows a more efficient way using knockout let me know!
As shown in my post here the GET action method Test(..) works fine when using foreach loop in the corresponding Test.chtml view but the POST action method Test(...) returns null. But, as mentioned by many users, the foreach is not reliable for POST method. So, I decided to use the for loop as shown below. But that returned unexpected results in the view since, according to this post, in a foreachloop type casting is done automatically but in for loop you have to type cast the objects Model[i].BlogID etc to a proper class object type.
So, I decided to type cast the objects Model[i].BlogID etc to a BlogsWithRelatedPostsViewModel class object type as shown in the second version of Test.cshml view below; and this time the Test.cshtml view is displayng the correct records. But although the submit button in the view is sending a a valid model (ModelState.IsValid is true) the Model.Count is 0 that results in no update to database. Why Model.Count is 0 and how to correct it? As you can see below the html page source of the view is showing the name attributes of the tags matching the property values in the View Model.
Note: For complete code, please see this OP. I'm using ASP.NET Core with EF Core and Tag Helpers.
Test.cshtml view with for loop - without type casting the loop objects:
#model IList<ASP_Core_Blogs.Models.BlogPostViewModels.BlogsWithRelatedPostsViewModel>
#using ASP_Core_Blogs.Models.BlogPostViewModels
#{ ViewData["Title"] = "Index"; }
<div class="row">
<div class="col-md-12">
<form asp-controller="Blogs" asp-action="Test" asp-route-returnurl="#ViewData["ReturnUrl"]" method="post">
#{
IEnumerable<SelectListItem> yearsList = (IEnumerable<SelectListItem>)ViewBag.YearsList;
var currentlySelectedIndex = 0; // Currently selected index (usually will come from model)
}
<strong>Select a Post Year</strong>
<h6>Choose a year and a URL to begin:</h6>
<label>Year:</label><select asp-for="#currentlySelectedIndex" asp-items="yearsList"></select><input type="submit" class="btn btn-default" name="GO" value="GO" />
<table class="table">
<thead>
<tr>
<th></th>
<th></th>
<th>Url</th>
<th>Title</th>
<th>Content</th>
</tr>
</thead>
<tbody>
#for (int i=0; i< Model.Count(); i++)
{
<tr>
<td><input type="hidden" asp-for="#Model[i].BlogID" /></td>
<td><input type="hidden" asp-for="#Model[i]).PostID" /></td>
<td>
<input type="text" asp-for="#Model[i].Url" style="border:0;" readonly />
</td>
<td>
<input asp-for="#Model[i].Title" />
</td>
<td>
<input asp-for="#Model[i].Content" />
</td>
</tr>
}
</tbody>
</table>
<button type="submit" class="btn btn-default">Save</button>
</form>
</div>
</div>
Test.cshtml view with for loop objects being casted as BlogsWithRelatedPostsViewModel class objects:
#model IList<ASP_Core_Blogs.Models.BlogPostViewModels.BlogsWithRelatedPostsViewModel>
#using ASP_Core_Blogs.Models.BlogPostViewModels
#{ ViewData["Title"] = "Index"; }
<div class="row">
<div class="col-md-12">
<form asp-controller="Blogs" asp-action="Test" asp-route-returnurl="#ViewData["ReturnUrl"]" method="post">
#{
IEnumerable<SelectListItem> yearsList = (IEnumerable<SelectListItem>)ViewBag.YearsList;
var currentlySelectedIndex = 0; // Currently selected index (usually will come from model)
}
<strong>Select a Post Year</strong>
<h6>Choose a year and a URL to begin:</h6>
<label>Year:</label><select asp-for="#currentlySelectedIndex" asp-items="yearsList"></select><input type="submit" class="btn btn-default" name="GO" value="GO" />
<table class="table">
<thead>
<tr>
<th></th>
<th></th>
<th>Url</th>
<th>Title</th>
<th>Content</th>
</tr>
</thead>
<tbody>
#for (int i=0; i< Model.Count(); i++)
{
<tr>
<td><input type="hidden" asp-for="((BlogsWithRelatedPostsViewModel)#Model[i]).BlogID" /></td>
<td><input type="hidden" asp-for="((BlogsWithRelatedPostsViewModel)#Model[i]).PostID" /></td>
<td>
<input type="text" asp-for="((BlogsWithRelatedPostsViewModel)#Model[i]).Url" style="border:0;" readonly />
</td>
<td>
<input asp-for="((BlogsWithRelatedPostsViewModel)#Model[i]).Title" />
</td>
<td>
<input asp-for="((BlogsWithRelatedPostsViewModel)#Model[i]).Content" />
</td>
</tr>
}
</tbody>
</table>
<button type="submit" class="btn btn-default">Save</button>
</form>
</div>
</div>
A Portion of html generated by View after Submit:
<tr>
<td><input type="hidden" data-val="true" data-val-required="The BlogID field is required." id="BlogID" name="BlogID" value="1" /></td>
<td><input type="hidden" data-val="true" data-val-required="The PostID field is required." id="PostID" name="PostID" value="1" /></td>
<td>
<input type="text" style="border:0;" readonly id="Url" name="Url" value="blog1#test.com" />
</td>
<td>
<input type="text" id="Title" name="Title" value="Title1" />
</td>
<td>
<input type="text" id="Content" name="Content" value="Content1" />
</td>
</tr>
I want to get filtered items size related to the product
this is Method json that returns by Id
public ActionResult getProductSize(int prodID)
{
YAT.CMS.Shared.DAL.JAGUAREntities dbContext = new YAT.CMS.Shared.DAL.JAGUAREntities();
var getsize = (from a in dbContext.ProductSizes
where a.ProductID == prodID
select a.Description).ToList();
//var json_string = "{ Size: \"\" }";
return Json(getsize, JsonRequestBehavior.AllowGet);
}
and this is my View the section that hold category cart Description
#for (int i = 0; i < Model.Count; i++)
{
<tr class="odd">
<td class="imageCnt">
<a href="#Url.Action("ProductDetails", "Product", new { ID = #Model[i].Id })">
<img class="bp-image" alt="#Model[i].EN_Name" src="#Url.Content("~/Admin/Admin_Cataloge/Product/" + #Model[i].Photo)" />
</a>
<input type="hidden" name="[#i].ProductID" value="#Model[i].Id" />
</td>
<td class="titleCnt">
#Model[i].EN_Name
</td>
<td class="priceCnt">#Model[i].Price<span class="currency"> </span>
<input id="#("Price" + i.ToString())" value="#Model[i].Price" type="hidden" />
</td>
<td class="quantityCnt">
<span class="basketQuantityCnt">
<input data-val="true" data-val-number="The field Quantity must be a number." data-val-required="The Quantity field is required." id="#("Quantity" + i)" name="[#i].Quantity" onkeypress="return restrictCharacters(this, event);" type="text" value="1"/>
<a class="deleteBtn" href="#" onclick="deleteBasketString(this,#Model[i].Id);"></a>
<a class="updateBtn" href="#" onclick="Calculate()"></a>
<a class="bq-plus" onclick="IncreaseQuantity(this);Calculate();"></a>
<a class="bq-minus" onclick="DecreaseQuantity(this);Calculate();"></a>
</span>
</td>
<td class="prodsize">
<select id="selectNumber">
<option>Choose a number</option>
//Here where i want to add Filtered Size for item
</select>
</td>
<td class="subTotalCnt" id="#("total" + i)">#Model[i].Price<span class="currency"> </span>
</td>
</tr>
total += Model[i].Price;
}
how to do this in the Commented in view where I want to do this?
I have a dynamic forms with checkbox/radio-button lists & matrices:
Following code renders checkbox list:
#foreach (var sq in Model.SubQuestions)
{
<label>
<input type="hidden" name="answerResult.index" value="#sq.Id" />
<input type="checkbox" name="answerResult[#sq.Id].SubQuestionId" value="#sq.Id" />
#sq.Label.Name
</label>
}
radio-button list:
<input type="hidden" name="answerResult.index" value="#Model.Id" />
#foreach (var sq in Model.SubQuestions)
{
<label>
<input type="radio" name="answerResult[#Model.Id].SubQuestionId" value="#sq.Id" />
#sq.Label.Name
</label>
}
My POST-action in controller:
[HttpPost]
public ActionResult PassageSurvey(int surveyId, int surveyPageIndex, IList<AnswerResult> answerResult)
where IList<AnswerResult> is an auto-bound collection from my form. I get only items that were checked/selected. Everything is going well.
Now I need to get the same collection from checkbox/radio-button matrices.
Radio-button matrix:
<table width="100%">
<tr>
<th></th>
#foreach (var av in Model.AnswerVariants)
{
<th style="text-align: center;">
<label>#av.Label.Name</label>
</th>
}
</tr>
#foreach (var sq in Model.SubQuestions)
{
<tr>
<td>
<label>#sq.Label.Name</label>
<input type="hidden" name="answerResult.index" value="#sq.Id" />
<input type="hidden" name="answerResult[#sq.Id].SubQuestionId" value="#sq.Id" />
</td>
#foreach (var av in Model.AnswerVariants)
{
<td align="center">
<input type="radio" name="answerResult[#sq.Id].AnswerVariantId" value="#av.Id" />
</td>
}
</tr>
}
</table>
Checkbox matrix:
<table width="100%">
<tr>
<th></th>
#foreach (var av in Model.AnswerVariants)
{
<th style="text-align: center;">
<label>#av.Label.Name</label>
</th>
}
</tr>
#foreach (var sq in Model.SubQuestions)
{
<tr>
<td>
<label>#sq.Label.Name</label>
</td>
#foreach (var av in Model.AnswerVariants)
{
<td align="center">
<input type="hidden" name="answerResult.index" value="#sq.Id" />
<input type="hidden" name="answerResult[#sq.Id].AnswerVariantId" value="#sq.Id" />
<input type="checkbox" name="answerResult[#sq.Id].SubQuestionId" value="#sq.Id" />
</td>
}
</tr>
}
</table>
POST-action in controller always the same.
Now from radio-button matrix (in current sample 3x3) IList<AnswerResult> gets always 3 items, depending on items that were selected in rows and columns.
But from Checkbox matrix (3x3) IList<AnswerResult> gets always all 9 items (regardless items were checked, hidden-inputs always have values)
But I want to get only items, that were checked. How could I change my checkbox-matrix template to solve this problem?
Finally I decided to handle this problem in my post-action instead. Removing items (checkboxes) that were not selected, and have nulls in answerResult[##].SubQuestionId fields.
What is wrong with the following code: I`m having the error message
Error 1 ; expected
<%if (Model.ReferenceFields != null)
{%>
<%int count = 1; %>
<%foreach (var referenceName in Model.ReferenceFields)
{%>
<%var value = "value"; %>
<%count++; %>
<%value = value + count.ToString(); %>
<tr>
<td><input type="hidden" name="Tests.Index" value='<%value%>' /></td>
<td><input type="text" name="Tests['<%value%>'].Value"/></td>
<td><input type="button" value= "Add" /></td></tr>
<%}
%>
<%}
%>
The basic problem is lines like this
<input type="hidden" name="Tests.Index" value='<%value%>' />
So you're wanting to write out the contents of value into the html but thats not the way to do it. It should be
<input type="hidden" name="Tests.Index" value='<% Response.Write(value); %>' />
or a shortcut for Response.Write is <%= so
<input type="hidden" name="Tests.Index" value='<%= value %>' />
ASP101 - Writing Your First ASP.NET Page
The other problem is that the formatting of your code is, quite frankly butt ugly and you're making it hard work for yourself when trying to read it. Try this instead.
<%
if (Model.ReferenceFields != null)
{
int count = 1;
foreach (var referenceName in Model.ReferenceFields)
{
var value = "value";
count++;
value = value + count.ToString();
%>
<tr>
<td><input type="hidden" name="Tests.Index" value='<%= value %>' /></td>
<td><input type="text" name="Tests['<%= value %>'].Value"/></td>
<td><input type="button" value= "Add" /></td></tr>
<%
}
}
%>