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.
Related
I am using the built-in template which comes with ASP.NET MVC 4. The View which is causing problem is: ManageLogins.cshtml
This is how the output looks like:
So, this particular user has connected both Google and Facebook external IDs to his account:
And this is the Built in MS Template which produces the above code (I have changed the appearance a bit, but the code is the same)
#foreach (var account in Model.CurrentLogins)
{
<div class="form-row">
#using (Html.BeginForm("RemoveLogin", "Manage"))
{
#Html.AntiForgeryToken()
<div>
#Html.Hidden("loginProvider", account.LoginProvider)
#Html.Hidden("providerKey", account.ProviderKey)
<input type="submit" class="btn btn-default" value="Remove" title="Remove this #account.LoginProvider login from your account" />
</div>
}
</div>
}
The problem is that this code generates 2 inputs with id=loginProvider, and this gives me the browser error:
How can solve this problem?
Since model binding occurs on name and not id, I decided to remove the id from the hidden field and just use the name attribute:
#using (Html.BeginForm("RemoveLogin", "Manage", FormMethod.Post, new { #class = "inline-form float-right" }))
{
#Html.AntiForgeryToken()
<div>
<input type="hidden" name="LoginProvider" value="#Model.LoginProvider" class="d-none login-provider" />
<input type="hidden" name="ProviderKey" value="#Model" class="d-none provider-key" />
<input type="submit" class="btn btn-default" value="Remove" title="Remove this #account.LoginProvider login from your account" />
</div>
}
You can use an overload of the helper that allows you to provide htmlAttributes.
e.g.
for (int i = 0; i < 3; i++)
{
#Html.Hidden("foo", $"value{i}", new { id = $"foo{i}"})
...
}
Caveat: while the sample above shows how you can do whatever you want for name, value, id, etc., it may affect validation (and force you to customize that too). In your specific use case however, since it's just id, it should be fine.
How to create a form that has a search function (pull data from the database) AND a submit function (add data to the database) at the same time using the BeginForm() method? I am reviewing the overloads on MSDN and I don't seem to find one.
Code:
#using (Html.BeginForm()){
<table>
#*Bunch of textboxes and dropdown lists*#
</table>
<div id=" buttonHolder">
<input id="Search" type="button" value="Search" />
<input id="Reset1" type="reset" value="Reset" />
<input id="Submit1" type="submit" value="Add" />
</div>
}
You can use two approaches here:
handle onsubmit and fetch/save data with AJAX (you can do it even with Html.BeginForm but it's easier to go just with regular <form ...)
#using (Html.BeginForm("DoIt", "DoItAction", FormMethod.Post, new { onsubmit = "submitWithAjax(event); return false;" }))
create two separate forms with a different action/controller pair
I have a company object which has a list of branch objects,
my company view(residing in the company directory) has a strongly typed branch list view (residing in the branch directory)in it,
each branch in the branch view has a delete button which I want to post to a delete action in the branch controller.
at present the invoked delete action is the one in the company controller
(there is a delete action in both company and branch)
I believe I understand the reason it is doing what it is, however what is the best practice in this situation....
should the branch list partial view reside in the company or branch directory?
should the delete branch action reside in the company or the branch controller?
I would think the branch list should be in the branch directory and call the branch controller, but how do I get it to do this when the partial view is loaded into the company details View?
Hope that made sense,
Thanks,
Mark
<% foreach (var item in Model) { %>
<tr>
<td>
<form action="Edit" method="get">
<input type="submit" value="Edit" id="Submit1" />
<input type="hidden" name="id" value="<%= item.Id %>" />
</form>
|
<form action="Branch" method="get">
<input type="submit" value="Details" id="Submit2" />
<input type="hidden" name="id" value="<%= item.Id %>" />
</form>
|
<form action="BranchDelete" method="post">
<input type="submit" value="BranchDelete" id="Submit1" />
<input type="hidden" name="id" value="<%= item.Id %>" />
</form>
You need to surround each set of fields that you want submitted with a separate form tag. You can have more than one form tag per page. In fact, you might want each partial view to have its own form tag that submits to a different controller action.
Put the partial views wherever makes most sense. The file location has nothing to do with how the form is submitted back from the browser.
You can post to different controllers like this. One posts to the Branch controller and one posts to the Company controller.
<% using (Html.BeginForm("RemoveBranch", "Branch", FormMethod.Post, new { #class = "branchform" }))
{
Html.RenderPartial("~/Views/Branch/BranchView.ascx");
}%>
<% using (Html.BeginForm("RemoveCompany", "Company", FormMethod.Post, new { #class = "companyform" }))
{
Html.RenderPartial("~/Views/Company/CompanyView.ascx");
}%>
In each view or partial view, all you need is a submit button:
<input type="submit" value="Delete" />
in my route table I have this entry
routes.MapRoute(
"myRoute",
"route/{controller}/{action}/{id}/{start}/{end}",
new { controller = "Home", action = "Index", id = "", start="", end="" }
);
in my master page I have a line of code like so:
<%= Html.TextBox("foo", "bar") %>
If I access the page in the form of http://mysite.com/route/Home/Index/id/start/end the textbox renders OK with a value of "bar"
However if I access the page using the default parameters http://mysite.com/route/ the textbox does not have a value! In the emitted HTML it shows up like so:
<input id="foo" type="text" value="" name="foo"/>
it didn't set the value to "bar"...is this a bug? or is this not allowed in mvc master pages?
it should work fine
" <%= Html.TextBox("name", "Please enter your name...")%>
Output : < input id="name" name="name" type="text" value="Please enter your name..." />
<%: Html.TextBox("foo", "bar") %>
sometimes you need to force it to be a simple html attribute as follows
<%: Html.TextBox("foo", null,new{value="bar"}) %>
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.