Create a dynamic amount of columns for each item - asp.net-mvc

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

Related

MVC View change specified row bgcolor

In my MVC view, I am displaying list of data from my model in a table as shown below. How to make a specified row to change color when one of the field satisfy certain condition within my if-else statement? It does not seems like I can add a jquery inside my if-else there for the purpose...
My View:
...
<table>
...
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.field1)
</td>
<td>
#Html.DisplayFor(modelItem => item.field2)
</td>
<td>
#if(item.field3== 0){
//change this row to grey color
}
else {
#Html.DisplayFor(modelItem => item.field3)
}
</td>
....
</table>
You just need to do it up where you are outputting the row. One way would be:
<table>
...
#foreach (var item in Model) {
<tr style='background-color: #(item.field3 == 0 ? "gray" : "white");'>
<td>
#Html.DisplayFor(modelItem => item.field1)
</td>
<td>
#Html.DisplayFor(modelItem => item.field2)
</td>
<td>
#Html.DisplayFor(modelItem => item.field3)
</td>
}
....
</table>
There are all sorts of ways to construct that conditional, just depends on your scenario. But the key is that you can access the field3 property of the item object anywhere in that foreach loop, just like a normal C# foreach

Edit,Detail,Delete not Working in partial View

I am using Partial View for Searching after search Record and i want to delete record when i click on delete then open new page and show message delete successfully but i want to show this message in the same page not new page
this is my Controller
public string Delete(string id)
{
oj.Delete(id);
return "del Successfuly";
}
Here is my view
when i am click on delete link the open new page and show Successfully message
bu i want to show message in the same page
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.emp_id)
</td>
<td>
#Html.DisplayFor(modelItem => item.emp_fname)
</td>
<td>
#Html.DisplayFor(modelItem => item.emp_lname)
</td>
<td>
#Html.DisplayFor(modelItem => item.emp_dob)
</td>
<td>
#Html.DisplayFor(modelItem => item.emp_contact1)
</td>
<td>
#Html.DisplayFor(modelItem => item.emp_address1)
</td>
<td>
#Html.DisplayFor(modelItem => item.emp_cnic)
</td>
<td>
#Html.DisplayFor(modelItem => item.dept_id)
</td>
<td >
<div id="tdi">
#Ajax.ActionLink("Delete", "Delete", new { id = item.emp_id }, new AjaxOptions
{
UpdateTargetId = "#tdi",
HttpMethod = "Get",
InsertionMode = InsertionMode.Replace
})
</div>
You need install the jQuery AJAX Unobtrusive library:
If you don't have this libary in your project, don't works the ajax request, so, the link opened in another page.
Install:
Install-Package Microsoft.jQuery.Unobtrusive.Ajax
View:
#Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")

How do I use a #Html.ActionLink inside a for loop

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>

Templates can be used only with field access, property access in Razor

I got this error when I was creating a Edit and Templating for Product.
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
Edit In my Razor View:
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.Select(m => m.ProductID))
</th>
<th>
#Html.DisplayFor(model => model.Select(m => m.Name))
</th>
<th>
#Html.DisplayFor(model => model.Select(m => m.ListPrice))
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.ProductID)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.ListPrice)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.ProductID }) |
#Html.ActionLink("Details", "Details", new { id=item.ProductID }) |
#Html.ActionLink("Delete", "Delete", new { id=item.ProductID })
</td>
</tr>
}
</table>
Do you know what that mean and how to fix it? Thank you so much!
doesnt this just work?
<th> #Html.DisplayFor(model => model.ProductID) </th>
edit, try something like this:
<table id="myTable">
<tr>
<th>
#Html.DisplayNameFor(model => model.ProductID)
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.ProductID)
</td>
<td>
#Html.DisplayFor(modelItem => item.ProductID)
</td>
</tr>
}
</table>

ASP.NET MVC join tables view result error

im trying to join two table and show the results in a strongly-typed list view in with columns from two tables,
public ActionResult OrderDetails(int id) {
var query = from o in db.OrderDetails
join a in db.Albums on o.AlbumId equals a.AlbumId
where o.OrderId == id
select new OrderDetail() {
OrderId = o.OrderId,
AlbumId = o.AlbumId,
Quantity = o.Quantity,
UnitPrice = o.UnitPrice,
Album = new Album {
Title = a.Title
}
};
return View(query);
}
Here is my view
#model IEnumerable<QWERK.OrderDetail>
#{
ViewBag.Title = "OrderDetails";
}
<h2>OrderDetails</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.OrderId)
</th>
<th>
#Html.DisplayNameFor(model => model.AlbumId)
</th>
<th>
#Html.DisplayNameFor(model => model.Quantity)
</th>
<th>
#Html.DisplayNameFor(model => model.UnitPrice)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.OrderId)
</td>
<td>
#Html.DisplayFor(modelItem => item.AlbumId)
</td>
<td>
#Html.DisplayFor(modelItem => item.Quantity)
</td>
<td>
#Html.DisplayFor(modelItem => item.UnitPrice)
</td>
<td>
#Html.DisplayFor(modelItem => item.Album.Title)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.OrderDetailId }) |
#Html.ActionLink("Details", "Details", new { id=item.OrderDetailId }) |
#Html.ActionLink("Delete", "Delete", new { id=item.OrderDetailId })
</td>
</tr>
}
</table>
But i keep getting a NotSupportedException
Im pretty sure im getting the error because im using the OrderDetails Model but how can i get around this to show multiple columns from different tables???? any help would be greatly appreciated...thanks

Resources