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" })%>
开发者_JAVA百科 <%=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.
精彩评论