I have following code in my MVC2 view:
<tr class="edit" style="display:none">
<td>
<%= Html.DropDownList("drpFields", new SelectList(Model.Fields, "FieldID", "NiceName", whiteout.FieldID)) %>
</td>
<td>
<%= Html.DropDownList("drpStartTimeh", new SelectList(Model.Hours, whiteout.StartHour.Hour.ToString("0,0")))%>
<%= Html.DropDownList("drpStartTimem", new SelectList(Model.Minutes, whiteout.StartHour.Minute.ToString("0,0")))%>
<%= Html.DropDownList("drpStartTimet", new SelectList(Model.AMPM, whiteout.StartHour.Hour > 12 ? "PM" : "AM"))%>
-
<%= Html.DropDownList("drpEndTime", new SelectList(Model.Hours, whiteout.EndHour.Hour > 12 ? (whiteout.EndHour.Hour - 12).ToString("0,0") : whiteout.EndHour.Hour.ToString("0,0")))%>
<%= Html.DropDownList("drpEndTimem", new SelectList(Model.Minutes, whiteout.EndHour.Minute.ToString("0,0")))%>
<%= Html.DropDownList("drpEndTimet", new SelectList(Model.AMPM, whiteout.EndHour.Hour > 12 ? "PM" : "AM"))%>
</td>
<td>
<%= Html.DropDownList("drprepeat", new SelectList(Model.RepeatList,whiteout.Repeats))%>
</td>
<td>
Active
</td>
<td>
<a class="icon-button-cancel" href='<%: Url.Action("EditWhiteOut", "Settings", new {Id = whiteout.WhiteoutID}) %>'>
<img src='<%: Url.Content("~/static/Images/expanded.png") %>' alt="Delete this device" />
</a>
<a class="icon-button-success" href="#">
<img src="/static/images/gear.png" alt="Edit this device" /></a>
</td>
<td>
</td>
</tr>
I want to create an object of type Whiteout class and populate it with values selected by user from dropdowns and send to settingcontroller's EditWhiteout action method instead of passing only new {Id = whiteout.WhiteoutID}. How can I do this ?
Please suggest solution.
Thanks.
MVC provides some automatic mapping on form values from the view back into the controller action. Usually you would use a strongly type view i.e. at the top of the page you will have something
like
Control Language="C#" Inherits="System.Web.Mvc.ViewPage<namespace.Whiteout>
The names of the dropdown lists need to match those of the properties in the Whiteout class and the controller action will expect a return value of the model type i.e.
[HttpPost]
public ActionResult EditWhiteout(WhiteOut tseItem)
{ //method body here
The rest should just happen as if by magic. If you are getting confused by the dropdowns it is sometimes easier to replace them with simple inputs until you get the send/receive part working - then change to dropdown.
Related
I have an MVC table that I've created from database query results. The issue that I'm having is that I am trying to create links out of the first column (device/circuit) results that will pass to an .aspx page to display the load history for the circuit that was clicked. I have the links creating and passing to the .aspx page, but the data being displayed is always for the last item.CUIRCUIT returned from the query. Is there any way to retrieve the LinkText from the link that was clicked? Everything that I've seen so far uses static ActionLinks, not variable ones.
As mentioned above, I have tried using the variable item.CIRCUIT LinkText value, and have set a Global Variable to this value to pre-populate a dropdownlist on the .aspx page, but of course I only receive the last Circuit from the list that was returned; not the value of the link that was clicked.
MVC Index code
#model IEnumerable<Mvc.Models.Circuits_View>
#using Mvc.Variables;
…
<div style="max-height:335px;overflow:auto;">
<table class="table table-bordered table-condensed table-striped">
#foreach (var item in Model)
{
<tr>
<td>
#*#Html.DisplayFor(modelItem => item.CIRCUIT)*#
#Html.ActionLink(item.CIRCUIT, null, "AmpLoadDaily", Globals.ddl_cktval1 = item.CIRCUIT, null)
</td>
<td align="right">
#Html.DisplayFor(modelItem => item.IA)
</td>
<td align="right">
#Html.DisplayFor(modelItem => item.IB)
</td>
<td align="right">
#Html.DisplayFor(modelItem => item.IC)
</td>
<td align="right">
#Html.DisplayFor(modelItem => item.IG)
</td>
<td align="right">
#Html.DisplayFor(modelItem => item.KW)
</td>
<td align="right">
#Html.DisplayFor(modelItem => item.KVA)
</td>
<td align="right">
#Html.DisplayFor(modelItem => item.KVAR)
</td>
<td align="right">
#Html.DisplayFor(modelItem => item.PF)
</td>
<td>
#Html.DisplayFor(modelItem => item.STATUS)
</td>
</tr>
}
</table>
</div>
ASPX DropDownList
<asp:DropDownList runat="server" ID="ddlDevice1" DataSourceID ="SqlDataSource_ddlDevice1" DataTextField="CIRCUIT" DataValueField="CIRCUIT"
AppendDataBoundItems="true" AutoPostBack="true" Width="95" OnSelectedIndexChanged="ddlDevice1_SelectedIndexChanged">
<asp:ListItem Text="-Device1-" Value=" " Selected="True">
</asp:ListItem>
</asp:DropDownList>
CodeBehind
if (Globals.ddl_cktval1 != "")
{
ddlDevice1.SelectedValue = Globals.ddl_cktval1;
Globals.ddl_cktval1 = "";
}
I expect the value to be the name of the link clicked, (i.e. Device1, or Device23), not always DeviceLast, (unless that was the link clicked, of course).
It seems that I was able to use an Anonymous Type after all to collect the item.CIRCUIT information for the specific ActionLink desired.
#foreach (var item in Model)
{
<tr>
<td>
#*#Html.DisplayFor(modelItem => item.CIRCUIT)*#
#Html.ActionLink(item.CIRCUIT, null, "AmpLoadDaily", new { device = item.CIRCUIT }, null)
</td>
My issue must have come from getting the Request.QueryString correct in the code behind.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlDevice1.SelectedValue = Request.QueryString["device"];
I am now able to pre-populate the DropDownList on the ASPX page with the value provided from the MVC page. I just need to figure out how to have the SqlDataSource's, (that use the selected value as a parameter), re-runat="server" during the page load, after I set the SelectedValue of the DropDownList to the new value passed. The search continues …
In my application,I've to select the last td (which is an img) in table.Can anyone help me with this ?
HTML
<table>
<tbody>
<tr>
<td>
<td>
<a onclick="return confirm('Delete creative?')" href="delete.page?cid=47">
<a href="edit.page?id=47"><a href="?duplicateId=47">
<img title="Duplicate" src="/tracker/images/skin2/bolean.png">
</a>
</td>
</tr>
</tbody>
</table>
Implemenetd as below :
#browser.img(:src => "/tracker/images/skin2/bolean.png").click
#browser.img(:src => "/tracker/images/skin2/bolean.png").last.click
which is clicking on the first image.
When you do:
#browser.img(:src => "/tracker/images/skin2/bolean.png")
This returns the first matching element.
If you want to get all of the matching elements, you need to pluralize the method:
#browser.imgs(:src => "/tracker/images/skin2/bolean.png")
You will then get a collection of all images that have the specified src. You can then get the last one and click it similar to how Željko did it for tds.
#browser.imgs(:src => "/tracker/images/skin2/bolean.png").last.click
Try this:
#browser.tds.last.click
I'm new to asp.net mvc 2.0.I have a question about listing data from database using asp.net mvc.
Controller
public ActionResult ListEmployee() {
UserModels emp = new UserModels();
emp.EmployeeList = (List<tblEmployee_Employee>)Session["list"];
return View(emp);
}
Model
public class GetEmployee
{
public string name { get; set; }
public string sex { get; set; }
public string email { get; set; }
}
And I have the view page employee.aspx page but I do not know how to write code in this view page.
please help me to solve this problem.
Thanks,
In ASP.NET MVC views need to be strongly typed to the model you are passing to them. So in your case you are passing a UserModels instance to your view. Assuming you already have a master page and you want to display in a table the list of employees you might have something along the lines of:
<%# Page
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<AppName.Models.UserModels>"
%>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<table>
<thead>
<tr>
<th>Name</th>
<th>Sex</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<% foreach (var employee in Model.EmployeeList) { %>
<tr>
<td><%= Html.Encode(employee.name) %></td>
<td><%= Html.Encode(employee.sex) %></td>
<td><%= Html.Encode(employee.email) %></td>
</tr>
<% } %>
</tbody>
</table>
</asp:Content>
or even better, define a reusable display template that will automaticall be rendered for each item of the EmployeeList collection property (~/Views/Shared/DisplayTemplates/GetEmployee.ascx):
<%# Control
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<dynamic>"
%>
<tr>
<td><%= Html.DisplayFor(x => x.name) %></td>
<td><%= Html.DisplayFor(x => x.sex) %></td>
<td><%= Html.DisplayFor(x => x.email) %></td>
</tr>
and then in your main view simply reference this template:
<%# Page
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<AppName.Models.UserModels>"
%>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<table>
<thead>
<tr>
<th>Name</th>
<th>Sex</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<%= Html.EditorFor(x => x.EmployeeList)
</tbody>
</table>
</asp:Content>
Now you no longer need any foreach loops (as ASP.NET MVC will automatically render the display template if the property is a collection property following standard naming conventions) and also you have a reusable display template for your model.
This is related to ASP.Net MVC 2
I have a model (MoviesForAll) bound to my view and this model has a collection of class ICollection. I want my user to buy single ticket at a time by clicking submit button 'Buy this ticket' so I have placed a button besides each row representing the ticket details. This lead to following sequence
<%#Page Title="title" Language="C#" MasterPageFile="~/Views/Shared/MyMaster.Master"
Inherits="System.Web.Mvc.ViewPage<OnlineBooking.Movies.MoviesForAll>">
<Table>
<% for(i=0; i<Model.Tickets.count; i++)
{ %>
<tr>
<% using (Html.BeginForm(myaction(), FormMethod.Post, new {id="myform"}))
{ %>
<td>
<%= Model.tickets[i].ticketID %>
<%= Html.HiddenFor(m=>m.tickets[i].ticketID) %>
</td>
<td>
<%= Model.tickets[i].seatNo %>
<%= Html.HiddenFor(m=>m.tickets[i].seatNo) %>
</td>
...
...
<td>
<input type="submit" value="buy this ticket" runat="server">
</td>
<% } %>
</tr>
<% } %>
</table>
This creates a form for each row of tickets having single submit button in that form.
The problem is, when I submit first row (having index 0) by clicking the button, it gets submitted properly and I can see the values in controller method. But no other row gets through to controller even if I have used hidden fields to bind it.
Am I missing anything here specific to index or something?
Thanks in advance..
I would suggest having one form for all the rows, then on each row use the button element instead of an input as follows:
<button type="submit" value="<%= Model.tickets[i].ticketID %>" name="buyticket">Buy this ticket</button>
You should have a parameter on your controller action called buyticket and that should have the ticket id of the button which was clicked
How about the following:
<table>
<% for(i = 0; i < Model.Tickets.Count; i++) { %>
<tr>
<% using (Html.BeginForm("myaction", FormMethod.Post, new { id = "myform" })) { %>
<td>
<%= Model.tickets[i].ticketID %>
</td>
<td>
<%= Model.tickets[i].seatNo %>
</td>
...
...
<td>
<%= Html.Hidden("TicketId", Model.tickets[i].ticketID) %>
<%= Html.Hidden("SeatNumber", Model.tickets[i].seatNo) %>
<input type="submit" value="buy this ticket" />
</td>
<% } %>
</tr>
<% } %>
</table>
and then:
public class TicketToPurchaseViewModel
{
public int TicketId { get; set; }
public int SeatNumber { get; set; }
}
and the action:
[HttpPost]
public ActionResult myaction(TicketToPurchaseViewModel ticket)
{
...
}
Remark: notice that I have removed the runat="server" attribute from the submit button as well because this is something that you definitely don't want to see in an ASP.NET MVC application.
<% foreach (var item in Model) { %>
<tr>
<td>
<%: Html.ActionLink("Edit", "EditUser", "Profile", new { id = item.UserID },null)%>
</td>
<td>
<%: Html.ActionLink("NewUser", "Create", "Profile", new { id = item.Customer },null)%>
</td>
<td>
<%: item.UserName %>
</td>
I am getting the NewUser Tab with every Edit Tab n also Username tab..I just need it once newhere in the Page, How Can i do that..n this is been done in a
I'm not sure what you're asking but if you only want one edit link for the passed in model then you should use the Model object.
Update based on new information in question
You need to pull the new user action link out of the foreach and instead of using item you use Model instead. If your Model does not contain a Customer object you should modify your ViewModel to include it or pass it in using the ViewData.Customer = SomeCustomerValue and then instead of using the model you would use View.Customer in your aspx page like so
Use this method if you're passing in the Customer via the ViewData
<%: Html.ActionLink("NewUser", "Create", "Profile", new { id = View.Customer },null)%>
(in your controller you would do this)
public ActionResult SomeAction() {
ViewData.Customer = CurrentCustomer;
return View(ListOfUsers);
}
Use this method if your Model includes the Customer property
<%: Html.ActionLink("NewUser", "Create", "Profile", new { id = Model.Customer },null)%>
<% foreach (var item in Model.Users) { %>
<tr>
<td>
<%: Html.ActionLink("Edit", "EditUser", "Profile", new { id = item.UserID },null)%>
</td>
<td>
<%: item.UserName %>
</td>
</tr>
<% } %>