I'm trying to add a search box to a master page in an ASP.Net MVC web app. What's confusing me is how to properly implement it in the master page. So the user types in data into this search box, how should the data be handled from an MVC perspective?? I know I could use he master page's code behind, but I shouldn't. I'm currently trying to use a user control for this, but I'm not sure how to properly implement it and online resources seem to be limited. Would creating an HTML helper be best??
To summarize: Implement a search box in the MVC master page that directs to a different website and includes the user's query that they typed in the search box.
Is it better to use:
Master Page's codebehind
A user control
Or create a separate HTML Helper.
UPDATE:
Ok, per queen3's advice, I implemented a SearchController and used the HTML Helper BeginForm to generate a search box.
Controller action:
Function SearchWiki(ByVal q As String) As ActionResult
Return Redirect("http://home/search/Results.aspx?k=" & q & "&s=IT%20FAQ")
End Function
And in the Master Page:
<% Using Html.BeginForm("SearchWiki", "Search", FormMethod.Post)%>
<input type="text" name="q" />
<input type="submit" value="Search" />
<% End Using%>
But when I try to debug, the SearchWiki function never gets called and, as a result, nothing happens when I type in the search box and hit Search.
Forget about codebehind and user controls if you're going to use ASP.NET MVC. You need HTML, CSS, and JavaScript.
I suppose you want something like
<form action="<%= Url.Action("Index", "Search") %>" method="post">
<input type="text" name="q" />
</form>
With helpers it will be something like
<% Html.BeginForm("Index", "Search") %>
<input type="text" name="q" />
<% Html.EndForm() %>
Just put this into master page where appropriate in you site design. Then create SearchController to handle request, and return View() with search results. You may make form use GET instead of POST if you accept google-like search requests /Search?q=text.
The controller is very simple:
public class SearchController: Controller
{
public ActionResult Index(string q)
{
return View(SearchHelper.DoSearch(q));
// or return Redirect("http://site?q=" + q) if you want redirect
}
}
To summarize: Implement a search box
in the MVC master page that directs to
a different website and includes the
user's query that they typed in the
search box.
Seems like you want to use a different search provider. In this case you don't need any server-sided code at all... only pure html. I'll give you an example with Google:
<form id="search" action="http://www.google.com.br/search" method="GET">
<input type="text" name="q" />
<input type="submit" value="Submit" />
</form>
Just add this code on your MasterPage and we're done.
You can also add some JQuery to append the string "site:www.yoursite.com" to the search query. Doing so you can ask google to search the keywords on your site. The javascript code should be:
$("#search").submit(function(){
var input = $(this).find('input[name=q]');
var query = input.val() + ' site:www.yoursite.com';
input.val(query);
});
Related
Hy
What i want to do is to create a custom renderer for will_paginate which renders first, previous, next and last page and a input field where the user can type in the page number manually. I already have the links for first, last etc. but i stuck at the input field. I could create a form in the view but the input field has to be rendered between the previous and next links.
Does anyone know how to do this?
Thanks for your help
You can do this as a separate form (make sure it is a GET). All you
need is the one input element named page. Something as simple as this
should work (not all browsers may like the #). I dropped it into a
site I'm playing with now and it worked. Put it anywhere on your page.
You might have to make something more complicated if you need to
incorporate search terms.
<form action="#" method="get">
Go to page: <input type="text" name="page" value="" size="2"
maxlength="4" />
<input type="submit" name="btnSubmit" />
</form>
I have the following form
<form id="file_upload" action="/Upload/Save" method="POST" enctype="multipart/form-data">
<input type="text" id="txtProposalName" name="name" placeholder="Nome da Camiseta" />
<input type="text" id="txtProposalDesc" name="description" placeholder="Descrição da Camiseta"/>
<div class="fileupload-buttonbar">
<div class="progressbar fileupload-progressbar"></div>
<span class="fileinput-button">
Upload images
<input type="file" name="files[]" multiple />
</span>
</div>
<input type="hidden" id="imgID" name="imgID"/>
<input type="submit" id="postProposal"/>
Would call this action:
[HttpPost]
public JsonResult Save(string name, string description, string imgID)
{
return Json("a");
}
(This is the current implementation, it has no logic because I am still testing some things).
My problem is: when I click on my submit button, the action is called with the correct values, but when it returns, my browser redirects to /Upload/Save (which is the action URL).
Why is this happening? Is there any way to prevent it?
Thanks!
You can use an asynchronous call to your method (e.g. AJAX) to prevent the page from reloading.
You can use Ajax.BeginForm to prevent reload page.
As I can see you use full page reload with:
action="/Upload/Save"
Also you trying to post data to json action method in controller. You need all form submit make with jquery, most problems you will find with ajax file uploaders.
Added:
Also look at serialize() it will help you to collect all form input values.
Dont need to prevent it, just redirect the user in your server code .
I need to open another site in new tab from code behind in Asp.net MVC.
return Redirect("Url"); is used to open the another site within the same tab.
It doesn't really seem practical for the users, because after authenticating in the second tab, they have to refresh the first tab to see the effects.
The ReturnUrl property of FormsAuthentication seems to do what you want. When the user needs to log in, they are redirected to the login page, and after signing in they are redirected back.
If you are making extensive use of javascript and ajax, and want to keep the javascript variables of the current page but need to log in to do the ajax calls, there might be another solution. If the response of your ajax call is the unauthenticated header, open a lightbox or something like that with a username and password field. Use ajax post to the AccountController to sign in the user again. This way, the user is authenticated again, but you keep the javascript variables.
This can be done using javascript only. Try this.
<%
Response.Write '<script type="text/javascript">
window.open(url);
</script>'
%>
Hope it works.
if you call action from form and use input type of submit you can try
<input type="submit" formtarget="_blank" />
if you use link <a> or AjaxCall you can try
<a target="_blank"></a> or in ajax helper set property #target="_blank"
here is my code
cshtml
#using (Html.BeginForm("PersonsReport", "Reports"))
{
<br />
<div style="text-align: center;">
<input type="submit" formtarget="_blank" class="btn btn-primary" value="GetReport" style="width:100%;" />
</div>
<br />
}
controller.cs
public ActionResult PersonsReport()
{
return Redirect("/PersonsReport.aspx");
}
I am working with a struts2 based application
Inside my JSP page for form submission is it mandatory to use s:form (Predefined struts component )
Because when i tried this way it worked (calling the Action class under struts.xml )
<s:form action="HelloWorld" >
<s:submit />
</s:form>
But When I tried to use normal form submission as shown
<form action="HelloWorld">
<input type="Submit"/>
</form>
It isn't working , it gave me 404 error .
please tell me is it mandatory to use and for data submission ??
A struts form action and an HTML tag form action are different. You can use a standard HTML form tag with struts if you create a struts specific URL for example (off the top of my head):
if using in multiple places, generate the url in and call like this -
<s:url id="myActionUrl" action="HelloWorld" />
<form action="<s:property value="%{myActionUrl}" />">
<input type="Submit"/>
</form>
or using in a single instance -
<form action="<s:url id="myActionUrl" action="HelloWorld" />">
<input type="Submit"/>
</form>
You can often look at the page source in your browser to see what Struts generates and recreate it manually like this. You will often end up using additional struts tags such as property to retrieve values from your value stack, but it is useful at times, for instance when generating JavaScript code dynamically.
No, it's not mandatory to use any of the S2 tags, but as Russell says, you need to duplicate the correct action URL.
You also need to be a little careful when mixing-and-matching S2 form tags with non-S2 HTML form tags, because the default S2 theme adds additional HTML markup to the page; they don't just create form tags--the default theme uses table tags to lay out the form.
You can use s:form for form and
<input type="Submit"/>
can be replaced by
<button type="submit"/>
It's not about which to use,it is what you want from it.Struts2 tags provides additional capabilities to the form.
Please go through below two links to get the diffrence
1] http://struts.apache.org/release/2.1.x/docs/form-tags.html
2] http://www.w3schools.com/tags/tag_form.asp
some facilities such as namespace,tooltip,tooltipIconPath and many are provided by struts2 tags.
I have form area in my view. If I click button A, I want to submit to /Books/1 and if I click button B, I want to submit to /Books/2
How do I achieve this with MVC?
<form id="form1" name="form1" action="/Books/" method="get">
<input type="text" name="search" value="">
<input type="submit" name="id" value="1">
<input type="submit" name="id" value="2">
</form>
It sounds like what you want to do is call the Books Controller, with, say, the Search action. So for instance you might want to call /Books/Search/<search expression>/1, or /Books/Search/<search expression>/2, etc. (There's a few different ways you could be formatting these URLs, but it's mostly a matter of personal preference I think) If you want the URLs to appear as you've got them above (without the action in the URL), that can be accomplished with routing, something like this:
routes.MapRoute(
"Books",
"Books/{searchExpr}/{pageId}",
new { controller = "Books", action = "Search", searchExpr = "", pageId = 1 }
);
I think the main problem is that you're trying to use the WebForms PostBack everything paradigm in a situation where you're probably better off sending the information to the server in the URL or query string. The only time you're actually going to be posting form data here is when the user actually types something into the search box and clicks the Search button - at that point, the controller will pass the search expression to the appropriate View by stuffing it in ViewData, and from there, the View can pull it out and repopulate that textbox on the results page.
MVC Views can have multiple forms on a 'page', so just create separate sections and give each one their own form action.
<form id="form1" name="form1" action="/Books/1" method="get">
<!--...form fields-->
</form>
<form id="form2" name="form2" action="/Books/2" method="get">
<!--...form fields-->
</form>
I have never seen the ability to have a form field attached to two forms, seems like it wouldn't work. What you can do is put a hidden field in the second form which, on submission, grabs the information from the textbox in the first form.