I have aspx page. I dislay data in table. How I can get new values from this table in Controller. Or how I can generate grid/table another method in aspx page in mvc?
<table id="Products" class="Products">
<tr>
<th>ProductId</th>
<th>Productname</th>
<th>Quantity</th>
<th>UnitPrice</th>
</tr>
<% foreach (var item in Model.NorthOrderDetails)
{
%>
<tr>
<td><%: item.ProductID %></td>
<td><%: item.ProductName %></td>
<td><%: Html.TextBox("Quantity",item.Quantity) %></td>
<td><%: Html.TextBox("UnitPrice",item.UnitPrice) %></td>
<td> <%: Html.ActionLink("Update", "View2") %></td>
</tr>
<% } %>
</table>
The table should be wrapped in a form. Each text box should have a unique 'id' attribute. This can be done with a few simple changes to your code.
Something like this:
<form>
<table id="Products" class="Products">
<tr>
<th>ProductId</th>
<th>Productname</th>
<th>Quantity</th>
<th>UnitPrice</th>
</tr>
<% foreach (var item in Model.NorthOrderDetails)
{
%>
<tr>
<td><%: item.ProductID %></td>
<td><%: item.ProductName %></td>
<td><%: Html.TextBoxFor(m => item.Quantity) %></td>
<td><%: Html.TextBoxFor(m => item.UnitPrice) %></td>
</tr>
<% } %>
</table>
<button type="submit">Update</button>
</form>
Let us know if this helps!
Related
I fetch data by using a selectmethod and it works fine , the problem is that I don't want a new table for each record inserted. I'd like a new column to be added to the existing table. Something like the picture below
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<asp:GridView ID="FormView1" runat="server" ShowHeader="False" ItemType="MieleRepresentative.SteamBoiler" SelectMethod="GetSteamBoilerModels" RenderOuterTable="false" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table class="table-striped table-bordered">
<tr style="background-color: thistle!important;">
<td style="font-weight: bold;">Model</td>
<td><%# Eval("Model")%></td>
</tr>
<tr>
<td style="font-weight: bold">WorkingPressureLimit</td>
<td><%# Eval("WorkingPressureLimit")%></td>
</tr>
<tr>
<td style="font-weight: bold">TestPressureLimit</td>
<td><%# Eval("TestPressureLimit")%></td>
</tr>
<tr>
<td style="font-weight: bold">Electrical</td>
<td><%# Eval("Electrical")%></td>
</tr>
<tr>
<td style="font-weight: bold">Diameter</td>
<td><%# Eval("Diameter")%></td>
</tr>
<tr>
<td style="font-weight: bold">Height</td>
<td><%# Eval("Height")%></td>
</tr>
<tr>
<td style="font-weight: bold">Weight</td>
<td><%# Eval("Weight")%></td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
public IQueryable<SteamBoiler> GetSteamBoilerModels([QueryString("productID")] Guid productId)
{
var _mieleDbEntities = new MieleDBEntities();
return _mieleDbEntities.SteamBoilers.Where(boiler => boiler.FKProduct == productId);
}
If you want a table with an undefined number of columns (one for each record selected), probabily GridView is not your best bet.
In my opinion you should build dynamically your table in .aspx page in this way:
<% var steamBoilersList = GetSteamBoilerModels(...params...); %>
<table>
<!-- first row -->
<tr>
<% foreach (var singleSteamBoiler in steamBoilersList) { %>
<td>
<%= singleSteamBoiler.FIELD1 %>
</td>
</tr>
<!-- second row and so on -->
<tr>
<% foreach (var singleSteamBoiler in steamBoilersList) { %>
<td>
<%= singleSteamBoiler.FIELD2 %>
</td>
</tr>
</table>
I'm currently trying to paginate a list of records, however; after the last table, the page break leaves an empty page. The set up is an Aspx files that incorporates two ascx files to draw the table records.
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Table.ascx.cs" Inherits="%>
<br class="clear" /><br/>
<table class="report-table" cellspacing="0" cellpadding="2">
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("PatientBills") %>' OnItemDataBound="Repeater1_OnItemDataBound">
<HeaderTemplate>
<tr>
<td>Seq</td>
<td>Billing Id</td>
<td>Patient Id</td>
<td>Patient Name</td>
<td>Sex</td>
<td class="PrintNormalWrap">Admit Date</td>
<td class="PrintNormalWrap" style="width: 80px;">Social Security</td>
<td>DOB</td>
<td>POS</td>
<td>CPT</td>
<td>Mods</td>
<td style="width: 220px;">Diagnosis</td>
<td class="PrintNormalWrap">Referring Provider(UPIN)/ Comment</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="billRowShade<%# Container.ItemIndex % 2 %>">
<asp:TableCell ID="tableCellSequence" runat="server"><%# Container.ItemIndex + 1 %></asp:TableCell>
<td><%# FormatBillingId(Eval("Bill.BillingId")) %></td>
<td><%# FormatField(Eval("Patient.PatientId")) %></td>
<td id="printPatientName"><%# FormatField(Eval("Patient.LastName")) %>, <%# FormatField(Eval("Patient.FirstName")) %></td>
<td><%# FormatField(Eval("Patient.Sex")) %></td>
<td><%# FormatShortDate(Eval("Patient.DateOfAdmittance"))%></td>
<td><%# FormatField(Eval("Patient.SocialSecurityNumber")) %></td>
<td><%# FormatShortDate(Eval("Patient.DateOfBirth"))%></td>
<td><%# FormatField(Eval("Bill.Pos")) %></td>
<td><%# FormatField(Eval("Bill.CptCode")) %></td>
<td><%# FormatField(Eval("Bill.Mod")) %></td>
<td><%# FormatDiagnoses(Eval("Bill.Diagnoses"), Eval("Bill.BillDate"), Eval("Bill.SiteName")) %></td>
<td class="PrintNormalWrap"><%# FormatNotes(Eval("Patient.ReferringMd"), Eval("Patient.RefUpin"), Eval("Bill.Notes"), Eval("Bill.TimeEntry")) %> <%# FormatSupervisor( Eval("Bill.SupervisedByName"), Eval("Bill.DoctorName") ) %></td>
</tr>
<asp:TableRow ID="tableRowSpecialtyFieldDetails" runat="server" CssClass="specialtyFieldDetails">
<asp:TableCell ID="TableCell1" colspan="12" runat="server"><span id="spanSpecialtyFieldDetails" runat="server"></span></asp:TableCell>
</asp:TableRow>
</ItemTemplate>
</asp:Repeater>
</table>
<p style="page-break-after:always"></p>
<br/>
I have this table
<table id="Products" class="Products">
<tr>
<th>ProductId</th>
<th>Productname</th>
<th>Quantity</th>
<th>UnitPrice</th>
</tr>
<% for(int i=0; i < Model.NorthOrderDetails.Count; i++)
{
%>
<tr>
<td><%: Html.Label(Model.NorthOrderDetails[i].ProductID.ToString()) %></td>
<td><%: Html.Label(Model.NorthOrderDetails[i].ProductName) %> </td>
<td><%: Html.TextBoxFor(m => m.NorthOrderDetails[i].Quantity) %></td>
<td><%: Html.TextBoxFor(m => m.NorthOrderDetails[i].UnitPrice) %></td>
<td> <input type="submit" value="?" Name="qodel" /> </td>
</tr>
<% } %>
</table>
<% } %>
And I want to sent to controller the value of ProductID in line which I press button. How I can do it?
So without Javascript you are limiting yourself slightly here, I think the code below should work but I'm sure someone can poke a hole in it!
<table id="Products" class="Products">
<thead>
<tr>
<th>ProductId</th>
<th>Productname</th>
<th>Quantity</th>
<th>UnitPrice</th>
</tr>
</thead>
<tbody>
#{
for(int i=0; i < Model.NorthOrderDetails.Count; i++)
{
<tr>
<td>
#Html.Label(Model.NorthOrderDetails[i].ProductID.ToString())
</td>
<td>
#Html.Label(Model.NorthOrderDetails[i].ProductName)
</td>
<td>
#Html.TextBoxFor(m => m.NorthOrderDetails[i].Quantity)
</td>
<td>
#Html.TextBoxFor(m => m.NorthOrderDetails[i].UnitPrice)
</td>
<td>
#Html.ActionLink("Edit", "Vi")
</td>
<td>
<input type="submit" name="submitValue"
value="#Model.NorthOrderDetails[i].SomeValue"/>
</td>
</tr>
}
}
</tbody>
</table>
Then make sure that your controller action accepts the name attribute of the input as a parameter:
public ActionResult YourControllerMethod(string submitValue) {
....
}
The code would look something like that in MVC3+, <% is from MVC2.
I have the following code which is supposed to take some information from a C# page and display it inside the aspx page, but when I run it i provides me with the error:
"Expected class, delegate, enum, interface, or struct" at line 58 which is this one:
<asp:Repeater runat="server" ID="InboxRecords">
Can you please help me and tell me what could cause this problem?
<asp:Repeater runat="server" ID="InboxRecords">
<ItemTemplate>
<% if (inbox == true)
{%>
<tr>
<td><%#DataBinder.Eval(Container.DataItem, "Sending")%></td>
<td><%#DataBinder.Eval(Container.DataItem, "BA")%></td>
<td><%#DataBinder.Eval(Container.DataItem, "Date")%></td>
<td><%#DataBinder.Eval(Container.DataItem, "Title")%></td>
<td><%# if (test != Convert.ToString(DataBinder.Eval(Container.DataItem, "DocPath")))
{%>
Document
<%}
%>
</td>
<td><%#DataBinder.Eval(Container.DataItem, "Subject").ToString().Replace("\n","<br />") %></td>
<% if (_userTypeId == 1)
{%>
<td><a href="Notifications.aspx?Id=<%# DataBinder.Eval(Container.DataItem, "Id")%>&delete=1"><img src="img\delete.png" alt="Delete" title="Delete" /></td>
<%}
%>
<%}
%>
</tr>
</ItemTemplate>
</asp:Repeater>
I'm trying to render a partial ascx view within another view.
I have the following error however in my ascx file, and after some research I'm still in the dark!:
Type or namespace definition, or
end-of-file expectedend-of-file expected
Here is the code in DinnerForm.ascx
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<NerdDinner.Models.Dinner>" %>
<%: Html.ValidationSummary("Please Corrent the Errors and Try Again.") %>
<fieldset>
<legend>Fields</legend>
<table border="0">
<tr>
<td><%: Html.LabelFor(m => m.Title) %></td>
<td><%: Html.TextBoxFor(m => m.Title) %></td>
<td><%: Html.ValidationMessageFor(m => m.Title, "*") %></td>
</tr>
<tr>
<td><%: Html.LabelFor(m => m.EventDate) %></td>
<td><%: Html.TextBoxFor(m => m.EventDate) %></td>
<td><%: Html.ValidationMessageFor(m => m.EventDate, "*") %></td>
</tr>
<tr>
<td><%: Html.LabelFor(m => m.Description) %></td>
<td><%: Html.TextAreaFor(m => m.Description) %></td>
<td><%: Html.ValidationMessageFor(m => m.Description, "*") %></td>
</tr>
<tr>
<td><%: Html.LabelFor(m => m.Address) %></td>
<td><%: Html.TextBoxFor(m => m.Address) %></td>
<td><%: Html.ValidationMessageFor(m => m.Address, "*") %></td>
</tr>
<tr>
<td><%: Html.LabelFor(m => m.Country) %></td>
<td><%: Html.DropDownListFor(m => m.Country, ViewData["countries"] as SelectList)%></td>
<td><%: Html.ValidationMessageFor(m => m.Country, "*") %></td>
</tr>
<tr>
<td><%: Html.LabelFor(m => m.ContactPhone) %></td>
<td><%: Html.TextBoxFor(m => m.ContactPhone) %></td>
<td><%: Html.ValidationMessageFor(m => m.ContactPhone, "*") %></td>
</tr>
<tr>
<td><%: Html.LabelFor(m => m.Latitude) %></td>
<td><%: Html.TextBoxFor(m => m.Latitude) %></td>
<td><%: Html.ValidationMessageFor(m => m.Latitude, "*") %></td>
</tr>
<tr>
<td><%: Html.LabelFor(m => m.Longitude) %></td>
<td><%: Html.TextBoxFor(m => m.Longitude) %></td>
<td><%: Html.ValidationMessageFor(m => m.Longitude, "*") %></td>
</tr>
<tr>
<td><input type ="submit" value="Save" /></td>
</tr>
</table>
</fieldset>
<% } %>
And here is an example of how I am using it in a file called create.aspx
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<NerdDinner.Models.Dinner>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Host a Dinner
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Host a Dinner</h2>
<% Html.RenderPartial("DinnerForm"); %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
Notice how you have <% } %> at the bottom of the .ascx file? That is a closing bracket for <% using (Html.BeginForm()) {%> which you seem to have missed out.
Add
<% using (Html.BeginForm()) {%>
to just below the
<%: Html.ValidationSummary("Please Corrent the Errors and Try Again.") %>
Resulting form
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<NerdDinner.Models.Dinner>" %>
<%: Html.ValidationSummary("Please Corrent the Errors and Try Again.") %>
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<table border="0">
<tr>
<td><%: Html.LabelFor(m => m.Title) %></td>
<td><%: Html.TextBoxFor(m => m.Title) %></td>
<td><%: Html.ValidationMessageFor(m => m.Title, "*") %></td>
</tr>
<tr>
<td><%: Html.LabelFor(m => m.EventDate) %></td>
<td><%: Html.TextBoxFor(m => m.EventDate) %></td>
<td><%: Html.ValidationMessageFor(m => m.EventDate, "*") %></td>
</tr>
<tr>
<td><%: Html.LabelFor(m => m.Description) %></td>
<td><%: Html.TextAreaFor(m => m.Description) %></td>
<td><%: Html.ValidationMessageFor(m => m.Description, "*") %></td>
</tr>
<tr>
<td><%: Html.LabelFor(m => m.Address) %></td>
<td><%: Html.TextBoxFor(m => m.Address) %></td>
<td><%: Html.ValidationMessageFor(m => m.Address, "*") %></td>
</tr>
<tr>
<td><%: Html.LabelFor(m => m.Country) %></td>
<td><%: Html.DropDownListFor(m => m.Country, ViewData["countries"] as SelectList)%></td>
<td><%: Html.ValidationMessageFor(m => m.Country, "*") %></td>
</tr>
<tr>
<td><%: Html.LabelFor(m => m.ContactPhone) %></td>
<td><%: Html.TextBoxFor(m => m.ContactPhone) %></td>
<td><%: Html.ValidationMessageFor(m => m.ContactPhone, "*") %></td>
</tr>
<tr>
<td><%: Html.LabelFor(m => m.Latitude) %></td>
<td><%: Html.TextBoxFor(m => m.Latitude) %></td>
<td><%: Html.ValidationMessageFor(m => m.Latitude, "*") %></td>
</tr>
<tr>
<td><%: Html.LabelFor(m => m.Longitude) %></td>
<td><%: Html.TextBoxFor(m => m.Longitude) %></td>
<td><%: Html.ValidationMessageFor(m => m.Longitude, "*") %></td>
</tr>
<tr>
<td><input type ="submit" value="Save" /></td>
</tr>
</table>
</fieldset>
<% } %>
Remove the <% } %> from the bottom of your DinnerForm.ascx.
You've got an unpaired <% } %> at the end of your ascx file.
The form is missing. Looks like you omitted:
<% using( Html.BeginForm() ) { %>
from the top, just before the validation summary.