Can a partial view be used to do Ajax item updates? - asp.net-mvc

I have a table built from a list of defect codes.
Can part of each row load a sub-table item complete with submit buttons?
Sample table:
<table><tr>
<th>Code</th><th>Description</th>
<th>Impact to your customers</th>
<th>Impact to your associates</th>
<th>Save</th>
<th>Save Errors</th></tr>
Where the first 2 columns are populated from the lookup table, and the next 3 columns are a form so the user can set the values or update them from previous values.
Can I somehow have 3 TD items per row an individual Ajax form with the codeId embedded as a hidden value? What would my strongly typed view(s) inherit? the outer layer would inherit IEnumerable<DefectDTO>, and the partial views would inherit the AssessmentDTO type?
Here's the actual table I'm trying to get to function:
<table>
<tr>
<th>
Code
</th>
<th>
Description
</th>
<th>
Document
</th>
<th>
Customer Contact Required for Resolution
</th>
<th>
AssociateSeverity
</th>
<th>
ShareholderSeverity
</th>
<th>
CustomerSeverity
</th>
<th>
RegulatorySeverity
</th>
<th>
RootCause
</th>
<th>
Investor Requirements*
</th>
</tr>
<% foreach (var item in Model.DefectCodes) { %>
<tr>
<% using (Ajax.BeginForm("Create", new AjaxOptions() ))
{%>
<td>
<%= Html.Encode(item.Code)%>
</td>
<td>
<%= Html.Encode(item.Description)%>
</td>
<% Html.RenderPartial("Create",null, ViewData); %><!-- This is where the form is -->
<% } %>
</tr>
<% } %>
</table>

<% foreach (var item in Model.DefectCodes) { %>
<tr>
<% using (Ajax.BeginForm("Create", new AjaxOptions() ))
{%>
<!--should be easy like this!--><!--ur codeid here-->
<%=Html.HiddenFor(modelItem => item.codeid)%>
<td>
<%= Html.Encode(item.Code)%>
</td>
<td>
<%= Html.Encode(item.Description)%>
</td>
<% Html.RenderPartial("Create",null, ViewData); %><!-- This is where the form is -->
<% } %>
</tr>
<% } %>
</table>
its just solves ur hidden value! problem..
u dont need to put it into a TD cause u want it to be hidden so it will still get the value for u but will not be shown in form or in table or in view.. that way u can keep 3 TD and get more value than the shown on form.

Related

Table Column NO for Local Variable in MVC View Page

Possible for my question?
My Application in Product Table.
No Name
1 Apple
2 Orange
3 PipleApple etc....
This above table of No is Sorted By Product ID.But my user is wanted some time prodect table in above product item deleted.Then ID is not ascending.Therefore Number ascending for want local variable in MVC Table of Index.chtml.Possible ....?
#model IEnumerable<RenewableEnergyProjcet.Models.REHPDataModels.ProductTable>
<table >
<thead>
<th>
#Html.Label("No")
</th>
<th>
#Html.Label("Name")
</th>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
//this part for dynamic local variable assign
</td>
<td >
#Html.DisplayFor(modelItem => item.Name)
</td>
}
</tbody>
</table>

output sql with html structure

Is there a way to store the HTML structure in a SQL view and output it with the stored HTML structure via controller?
For e.g. Here is a sample HTML which I'd like to store in a SQL View and output via controller
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<vwStudent>>" %>
<html>
<body>
<table>
<tr>
<th>
Student ID
</th>
<th>
Name
</th>
<th>
GPA
</th>
<th>
Scholarship Amount
</th>
<th>
Eligible Date
</th>
<th>
Is Senior
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.Encode(item.StudentID) %>
</td>
<td>
<%= Html.Encode(item.FName) %>
</td>
<td>
<%= Html.Encode(item.GPA) %>
</td>
<td>
<%= Html.Encode(String.Format("{0:F}", item.ScholarshipAmount)) %>
</td>
<td>
<%= Html.Encode(String.Format("{0:g}", item.EligibleDate)) %>
</td>
<td>
<%= Convert.ToString(item.IsSenior) == "True" ? "Yes" : Convert.ToString(item.IsSenior) == "False" ? "No" : null%>
</td>
</tr>
<% } %>
</table>
</body>
</html>
Controller Action:
public ActionResult Students()
{
ViewData.Model = students.vwStudent.ToList();
return View();
}
65,000 records is FAR FAR too many to render on a single page... you should use pagination. There are libraries to help with this like this one: https://www.nuget.org/packages/PagedList.Mvc/

How to handle errors in Razor view engine?

How to handle errors in Razor (i.e. Null Exception)other than (try/catch)?
I do not want to insert (try/catch) block in every razor block.
Example (The (Model.ListofEntity) or (Item.Values) may be null and an exception might occur):
<table class="table">
<tr>
<th>
<div id="chkCheckAll" class="divCheckbox">
</div>
</th>
#foreach (var column in Model.ListofEntity.Columns)
{
int t = Convert.ToInt32(" ");
<th>
#column
</th>
}
</tr>
#foreach (CVMEntities.Item Item in Model.ListofEntity.Items)
{
<tr>
<td>
<div id=#Item.ID class="divCheckbox">
</div>
</td>
#foreach (var Value in Item.Values)
{
<td>
#Value
</td>
}
</tr>
}
</table>

Asp.net MVC 2 rendering view based on dropdown list

All,
I have the following model:
public class StateTaxes
{
public StateTaxes()
{
}
public int StateCode{get;set;}
public IList<BlendStateTaxRate> BlendStateTaxes{get;set;}
}
public class BlendStateTaxRate
{
public string BlendName{get;set;}
public int StateCode{get;set;}
public int BlendCode{get;set;}
public decimal TaxRate{get;set;}
}
My view consists of a dropdownlist that contains states. The initial entry in the dropdown list is --Select a State--, such that when a user navigates to /StateTaxes. The user is greeted with a dropdown list and the column headings for my taxes. I am using JQuery's change event to retrieve the taxes associated with a state when the user selects a valid state.
My question is what is the best way to populate the default view which only contains the column headings and not the eventual HTML. I guess the real crux of the issue is that the view doesn't offer create only view or edit.
The view is shown below:
<select id="StateList">
<option>--Select a State--</option>
</select>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm())
{ %>
<fieldset>
<legend>State Taxes</legend>
<table>
<thead>
<tr>
<th>
Blend Type
</th>
<th>
Tax Rate
</th>
<th>
Discount Rate
</th>
<th>
ExportFee Rate
</th>
<th>
Inspection Rate
</th>
<th>
PollTax Rate
</th>
<th>
TrustFund Rate
</th>
</tr>
</thead>
<% for (int i = 0; i < Model.BlendStateTaxes.Count; i++)
{ %>
<tr>
<td>
<%= Html.DisplayFor(m => m.BlendStateTaxes[i].BlendName)%>
</td>
<td>
<%= Html.EditorFor(m => m.BlendStateTaxes[i].TaxRate, new { disabled="true" })%>
<%=Html.ValidationMessageFor(m => m.BlendStateTaxes[i].TaxRate)%>
<%=Html.HiddenFor(m => m.BlendStateTaxes[i].BlendCode)%>
</td>
<td>
<%= Html.EditorFor(m => m.BlendStateTaxes[i].DiscountRate, new { disabled = "true" })%>
<%=Html.ValidationMessageFor(m => m.BlendStateTaxes[i].DiscountRate)%>
<%=Html.HiddenFor(m => m.BlendStateTaxes[i].BlendCode)%>
</td>
<td>
<%= Html.EditorFor(m => m.BlendStateTaxes[i].ExportFeeRate, new { disabled = "true" })%>
<%=Html.ValidationMessageFor(m => m.BlendStateTaxes[i].ExportFeeRate)%>
<%=Html.HiddenFor(m => m.BlendStateTaxes[i].BlendCode)%>
</td>
<td>
<%= Html.EditorFor(m => m.BlendStateTaxes[i].InspectionRate, new { disabled = "true" })%>
<%=Html.ValidationMessageFor(m => m.BlendStateTaxes[i].InspectionRate)%>
<%=Html.HiddenFor(m => m.BlendStateTaxes[i].BlendCode)%>
</td>
<td>
<%= Html.EditorFor(m => m.BlendStateTaxes[i].PollTaxRate, new { disabled = "true" })%>
<%=Html.ValidationMessageFor(m => m.BlendStateTaxes[i].PollTaxRate)%>
<%=Html.HiddenFor(m => m.BlendStateTaxes[i].BlendCode)%>
</td>
<td>
<%= Html.EditorFor(m => m.BlendStateTaxes[i].TrustFundRate, new { disabled = "true" })%>
<%=Html.ValidationMessageFor(m => m.BlendStateTaxes[i].TrustFundRate)%>
<%=Html.HiddenFor(m => m.BlendStateTaxes[i].BlendCode)%>
</td>
</tr>
<%} %>
</div>
</table>
</fieldset>
<input id="saveStateTaxes" type="submit" value="Save" />
I think whatever you have done is correct as per your requirement except for the unwanted closing div tag before closing table tag.
If you can write an ajax service to fetch the records I would recommend you to use client side templates to bind the table. This way you have complete control of the view and also it will improve the page performance as well.

MVC checkbox event in grid

I am a new bee in MVC world. I have a scenario like, i have an grid with a checkboxs as one column. When i click on checkbox an event would fire and it would update some values in database.
I am using Razor engine.
<table>
<tr>
<th>
ID
</th>
<th>
PName
</th>
<th>
PDescription
</th>
<th>
PSerialNo
</th>
<th>
PPrice
</th>
<th>
PActive
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#item.ID
</td>
<td>
#Html.ActionLink(#item.PName, "Edit", new { id = #item.ID })
</td>
<td>
#item.PDescription
</td>
<td>
#item.PSerialNo
</td>
<td>
#String.Format("{0:c}", item.PPrice)
</td>
<td>
#Html.CheckBox("chkActiveItem", item.PActive)
</td>
</tr>
}
here the content is showed in the grid. Here when i click on this check box i want to update the flag in the database.
How will i do it?
Please help.
foreach doesn't work in this case. You need to use for loop and then changes will get picked up on server side on post.
if you want to do ajax call then simply store id of each item within its row and pass that as parameter during ajax call to identify checked item on server side.

Resources