Basically I want my radio buttons to be checked depending on a value from my model view control, below is some sample code and my attempt so far:
<tr>
<td>
<label for="brakepads" class="label3">Brake Pads:</label></td>
<%= Html.TextBox("brakepads", Model.brakepads, new {style="display:none" })%>
<%= Html.ValidationMessage("brakepads", "*") %>
<td><label for="brakefluid" class="label4" >Passed:</label>
<asp:RadioButton ID="RadioButton15" runat="server" GroupName="b" value="True" Checked="<%= Model.brakepads.Value %>" onclick="brakepads.value=(this.value)" /></td>
<td><label for="brakefluid" class="label4" >Failed:</label>
<asp:RadioButton ID="RadioButton16" runat="server" GroupName="b" value="False" Checked="<% Model.brakepads.Value %>" onclick="brakepads.value=(this.value)"/></td>
</tr>
I get the following at runtime: Cannot create an object of type 'System.Boolean' from its string representation '<%= Model.brakepads.Value %>' for the 'Checked' property.
I tried using (bool) to cast, but that didn't work either, so I'm unsure of what to do next. My only concern is that occasionally the bool will be null, if that won't break functionality then it's fine.
How to do a boolean radio button list in ASP.NET MVC 3 razor C#
What we want the html to be
Apply discount:
<input type="radio" name="apply_discount" value="yes" /> Yes
<input type="radio" name="apply_discount" value="no" /> No
We need a model property
public bool ApplyDiscount { get; set; }
Now all we have to do is convert the html to razor
Apply discount:
#Html.RadioButtonFor(m => m.ApplyDiscount, true) Yes
#Html.RadioButtonFor(m => m.ApplyDiscount, false) No
I hope this helps.
You are mixing WebForms controls (RadioButton) together with ASP.NET MVC helpers (Html.TextBox), I think it's a dangerous practice.
You could try using Html.RadioButton() helper method to render a radio button:
<td><label for="brakefluid" class="label4" >Passed:</label>
<%= Html.RadioButton("brakefluid", true, Model.brakepads.Value) %></td>
<td><label for="brakefluid" class="label4" >Failed:</label>
<%= Html.RadioButton("brakefluid", false, !Model.brakepads.Value) %></td>
Either you use Html.RadioButton or have another MVC hidden field and bind Model.brakepads to it. Use javascript to set radio button's Checked property.
Related
In an ASP.Net Core web app, I'm passing a List<Parameter> to a view via ViewBag.Parameters. The view also has a SelectParameterViewModel that consists of one property only, an int ParameterId. The objects are then displayed in a table like so (I skipped straight to tbody as the view works fine from a cosmetic perspective):
#model WebApp.Models.ReportViewModels.SelectParameterViewModel
[...]
<tbody>
#foreach (var item in ViewBag.Parameters)
{
<tr>
<td class="col-xs-1">
<form asp-controller="Report" asp-action="Launch">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input asp-for="#Model.ParameterId" type="hidden" value="#item.Id" />
<input type="submit" class="btn btn-primary btn-sm" value="Select" />
</form>
</td>
<td class="col-xs-8">#item.Description</td>
<td class="col-xs-3">
<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>
I would like the user to click on the button of the chosen table-row and submit either that row's Parameter (preferred) or the Parameter's Id, like I tried to do in this case. I would then do the same with the <a> tags on the third column. The point is that I don't want the user to simply type /Report/Launch/id and run the report, as each user would have its own sets of parameters and should not be allowed to use any of the others', and the ViewBag only contains the list of its Parameter. I can change the view model and/or the receiving controller's argument depending on the solution, both would work. I tried a few versions of the above html, but none would work, while the above returns ParameterId with value 0.
I couldn't find a similar scenario in other SO questions. There were some solutions that involved JavaScript and, correct me if I'm wrong, but that would still show the ParameterId value in the webpage source. While that would be sort-of-ok in this specific case, I have other cases where I definitely would not want that to happen.
What would be an elegant solution to achieve this? Thank you!
How can I create a column in a table in MVC4 to show an uneditable checkbox instead of the word true.
#foreach (var m in Model)
{
<tr>
<td>#m.UserLogin
</td>
<td>#m.UserEmployeeName
</td>
<td>#m.UserAccessRightsID
</td>
<td>#m.SystemManager #*CHECK BOX SHOULD BE HERE*#
</td>
Here is a screen shot of what I am currently getting.
<input type="checkbox" checked='#m.SystemManager' disabled="disabled" />
It uses the Razor V2 feature which removes the attribute if the value is null or false.
You can read more about it here
I am trying to create a simple search box that results in something like http://www.example.com/Search?s=searchTerm I have the routing setup so that it accepts a url like this, and does the right thing. The problem I'm having is getting the form to create the querystring. I have tried many variations of:
<% using (Html.BeginForm("Search", "Home", FormMethod.Get, new { ???? })) {%>
<input id="submitSearch" class="searchBox" type="text" runat="server"/>
<input type="submit" value="Search" /> <%} %>
I'm not sure how to setup the Html.BeginForm so it grabs the submitSearch value and passes it to /Search?s=valueHere. This seems like I am missing something easy.
You need to set name on the input box to s.
<% using (Html.BeginForm("Search", "Home", FormMethod.Get, new { })) { %>
<input id="s" name="s" class="searchBox" type="text" />
<input type="submit" value="Search" />
<% } %>
Also, note that I changed the id to s as well, since common practice is to have the same value for name and id. However, it is only the name attribute that affects the query string name in the request.
And as David noted in a comment, the runat="server" is not needed in ASP.NET MVC.
I am using asp.net mvc. I have generated a view that retrieve all unapproved users in the asp.net membership table. I have put checkboxes next to them for someone to bring up a view. The goal is that someone should be able to check certain checkboxes, hit save and that will go back to asp.net membership and change the IsApprove flag to true for these users.
How do i extract what fields are set as true when i am in the controller class?
here is the view code:
<% using (Html.BeginForm()) {%>
<table id="hor-zebra" border = 2>
<tr><td>User</td><td>Approve</td>
</tr>
<%
MembershipUserCollection membership = (MembershipUserCollection)ViewData["UnapprovedUsers"];
foreach (MembershipUser member in membership)
{
%><tr><td>
<%=Html.Encode(member.UserName) %> </td><td>
<%= Html.CheckBox("Approve:" + member.UserName, false) %>
</td></tr>
<%
}
%>
</table>
<input type="submit" value="Save" /><% } %>
Here is the controller code:
[AcceptVerbs(HttpVerbs.Post)]
public void ApproveUsers(FormCollection formCollection)
{
Console.Write("I have not idea how i can determine which checkboxes are checked");
}
First of all, be careful with Html.Checkbox(). It does not just render a checkbox, but a hidden field as well. Each user (such as "username1" in this case) will have the following rendered:
<input id="Approve: username1" name="Approve: username1" type="checkbox" value="true"/>
<input name="Approve: username1" type="hidden" value="false" />
This question and selected answer discus it in more detail. One option is to just write the html yourself for this, such as:
<input type="checkbox" name="<%=member.UserName%>" />
When this gets posted to your Controller you can retrieve it by Request.Form[member.Username]. If it's null, the box wasn't checked. If it has a value of "On", it was.
I'm trying to implement something like this:
<div>
<table>
<thead>
<tr>
<td>Port name</td>
<td>Current port version</td>
<td>New port version</td>
<td>Update</td>
</tr>
</thead>
<% foreach (var ip in Ports) { %>
<tr>
<td>
<%= ip.PortName %>
</td>
<td>
<%= ip.CurrentVersion %>
</td>
<td>
<%= ip.NewVersion %>
</td>
<td>
<asp:Button ID="btnUpdate" runat="server" Text="Update" CommandArgument="<% ip.PortName %>" />
</td>
</tr>
<% } %>
</table>
</div>
The button's CommandArgument property is where my code complains about not being able to resolve symbol ip. Is there any way to do what I'm trying to do?
You don't want to use a Webforms button in ASP.NET MVC. MVC is a completely different way of working, and you no longer have the WebForms abstraction.
You have 2 different options you can either replace your asp:Button with an input tag or use a standard hyperlink instead. If you use the input option then you will need to wrap in a form element. The form action should point to a Controller action.
You can't use webform controls in ASP.NET MVC in a trivial manner because they rely on things that are stripped out in MVC. Instead you add a button in two ways, both using the HtmlHelper on the ViewPage:
You can add a button in a form, which is easily handeled in a controller if you have a form for each single button:
<% using(Html.BeginForm("Update", "Ip", new {portName = ip.PortName} )) { %>
....
<input name="action" type="submit" value="Update">
<% } %>
BeginForm() will default to the same controller and action as the view was created from. The other way is to add a link instead, which is more fitting to your example of iterating through a list. For example lets say you have IpController
<%= Html.ActionLink("Update IP", "Update", "Ip",
new {
portName = ip.PortName
})
%>
The link will go to the Update action in IpController with the given portName as parameter. In both cases you'll need this action in IpController:
public ActionResult Update(string portName) {
// ...
}
Hope this helps.
I think you have to enclose your block in Form tags ans runat=server.
FWIW,
I think this text is missing an equals sign:
CommandArgument="<% ip.PortName %>"
Should be
CommandArgument="<%= ip.PortName %>"