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.
Related
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/
This question already has answers here:
#Html.Action in Asp.Net Core
(10 answers)
Closed 6 years ago.
I am new to .Net and MVC and I seem to hit a problem that I can´t fix. I am trying to follow a couple of tutorials but I have failed when it comes to partial views. I looked up a lot of answers here and on different websites and still nothing works. I can´t figure out what I am doing wrong.
This is my partial controller:
public class PartialController : Controller
{
public ActionResult _PartialView()
{
var model = new Partial();
model.Count = 2;
return PartialView(model);
}
}
And this is my Partial View:
#model WebApplication1Tutorial.Models.Partial
<div>
The magic number is:
#Html.DisplayFor(model => model.Count)
</div>
I call this view in Index.cshtml by the following command:
<div>
#Html.Partial("/Views/Partial/_PartView.cshtml",new Partial())
</div>
And finally this is the model:
public class Partial
{
public int Count { get; set; }
}
Everything that I tried comes up with the webpage displaying the text and the number 0. I really have no idea what else to do. I know it is something easy but I can´t see it.
Thanks for the help.
Edit. The code for the index view:
#model MovieGenreViewModel
#{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<form asp-controller="Movies" asp-action="Index" method="get">
<p>
<select asp-for="movieGenre" asp-items="Model.genres">
<option value="">All</option>
</select>
Title: <input type="text" name="SearchString" >
<input type="submit" value="Filter" />
</p>
</form>
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.movies[0].Genre)
</th>
<th>
#Html.DisplayNameFor(model => model.movies[0].Price)
</th>
<th>
#Html.DisplayNameFor(model => model.movies[0].ReleaseDate)
</th>
<th>
#Html.DisplayNameFor(model => model.movies[0].Title)
</th>
<th>
#Html.DisplayNameFor(model => model.movies[0].Rating)
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.movies) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
#Html.DisplayFor(modelItem => item.Price)
</td>
<td>
#Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
<a asp-action="Edit" asp-route-id="#item.ID">Edit</a> |
<a asp-action="Details" asp-route-id="#item.ID">Details</a> |
<a asp-action="Delete" asp-route-id="#item.ID">Delete</a>
</td>
</tr>
}
</tbody>
</table>
<div>
#{ Html.RenderAction("_PartialView"); }
</div>
When you pass your partial like with the #Html.Partial, you also pass a new Model and the _PartialView() method is not invoked so the model is not populated with the value 2.
You could use #Html.Action helper:
<div>
#Html.Action("/Views/Partial/_PartView.cshtml")
</div>
I have an existing table with many columns wherein I wanted to do a searchbox for the data inside the table.
I am using an ASPX page.
Can anyone give me an example so that I could at least refer my project there ?
Thanks.
<table class="table table-bordered">
<tr>
<th>
<%: Html.ActionLink("Employee", "Index", new { sortOrder = ViewBag.NameSortParm }) %>
</th>
<th>
<%: Html.ActionLink("Department", "Index", new { sortOrder = ViewBag.DeptSortParm }) %>
</th>
<th>
<%: Html.ActionLink("Local", "Index") %>
</th>
<th>
<%: Html.ActionLink("Position", "Index") %>
</th>
<th>
<%: Html.ActionLink("DirectLine", "Index") %>
</th>
<th>
<%: Html.ActionLink("Plant", "Index") %>
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<h6> <%: Html.DisplayFor(modelItem => item.Emp_Name) %></h6>
</td>
<td>
<h6> <%: Html.DisplayFor(modelItem => item.Emp_Dept) %> </h6>
</td>
<td>
<h5> <b><%: Html.DisplayFor(modelItem => item.Emp_Local) %></b></h5>
</td>
<td>
<h6> <%: Html.DisplayFor(modelItem => item.Emp_Position) %> </h6>
</td>
<td>
<h6> <%: Html.DisplayFor(modelItem => item.Emp_DirectLine) %> </h6>
</td>
<td>
<h6> <%: Html.DisplayFor(modelItem => item.Emp_Entity) %> </h6>
</td>
</tr>
<% } %>
</table>
and my controller
public ActionResult Index(string nameString, string local, string dept )
{
ViewBag.Title = "Phone Directory";
List<PD_Employee> model = db.PD_Employee.ToList();
} ....... This is what I'm gonna do.
I'll gonna give bounty for this after 2 days !
If you need to get data on basis of either nameString,local or dept than you have to use or condition in linq.
List<PD_Employee> model = db.PD_Employee.Where(x => (x.Name == nameString || x.Local == local || x.Dept = dept)).ToList();
In some of my tables in the Index View, some roles can see more of the table than others.
So this means I should hide some columns
I've done this quick and dirty as such:
<% bool codesZichtbaar = Roles.IsUserInRole("Admin") || Roles.IsUserInRole("Algemeen Beheer") || Roles.IsUserInRole("Secretariaat"); %>
<table>
<tr>
<th>
</th>
<th>
Adres
</th>
<th>
Instellingsnr.
</th>
<% if (codesZichtbaar) { %>
<th>
Codes
</th>
<%} %>
<th>
IKON
</th>
<th>
Datum Van-Tot
</th>
<th>
</th>
</tr>
<% foreach (var item in Model) { %>
<tr class="inst<%: item.actieveSchool?"active":"inactive" %>">
<td>
<% Html.RenderPartial("buttons", new MVC2_NASTEST.Models.ButtonDetail() { RouteValues = new { id = item.Inst_ID }, edit = false, details = true, delete = false, takeOptionalsFromUrl = true }); %>
</td>
<td>
<%: item.INST_NAAM%><br />
<%: item.STRAAT%><br />
<%: item.POSTCODE%>
<%: item.GEMEENTE%>
</td>
<td>
<%: item.DOSSNR%>
</td>
<% if (codesZichtbaar) { %>
<td>
Gebruiker:
<%: item.Inst_Gebruikersnaam%><br />
C:
<%: item.Inst_Concode%><br />
D:
<%: item.Inst_DirCode%>
</td>
<%} %>
<td>
T:
<%: item.INST_TYPE%><br />
Inst_REF:
<%: item.INST_REF%><br />
Loc_REF:
<%: item.INST_LOC_REF%><br />
Loc_Nr:
<%: item.INST_LOCNR%>
</td>
<td>
<%: String.Format("{0:d}", item.DATUM_VAN)%>
-
<%: String.Format("{0:d}", item.DATUM_TOT)%>
</td>
<td>
<a href='<%= Url.Action("Links", "Instelling", MVC2_NASTEST.RouteValues.MergeRouteValues(MVC2_NASTEST.RouteValues.optionalParamters(Request.QueryString), new {id=item.Inst_ID})) %>'
title="Linken">
<img src="<%= Url.Content("~/img/link.png") %>" alt="Link" width="16" /></a>
</td>
</tr>
<% } %>
</table>
but this is MVC, my code is dry, what is the correct way to do this? or, what is a cleaner way to do this.
I would write a Html helper for this. It could look something like this:
public bool IsInRole(this HtmlHelper instance, params string[] roles)
{
var user = html.ViewContext.HttpContext.User;
foreach(var role in roles)
{
if(user.IsInRole(role))
return true;
}
return false;
}
This function will return true if user is in any of the provided roles. You would use it like this:
<tr>
<td>First - Visible to all</td>
<% if(Html.IsInRole("Admin", "Algemeen Beheer", "Secretariaat")) { %>
<td>Only for privileged users</td>
<% } %>
<td>Last - Visible to all
</tr>
This is a light, clean and reusable way of doing it.
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.