I've made a mvc4 project in Visual Studio Express 2012 for web. And there I've made a search function. And a view to show the result.
So normally I would have added this to the _Layout.cshtml.
if (Request["btn"] == "Search")
{
searchValue = Request["searchField"];
if (searchValue.Length > 0)
{
Response.Redirect("~/Views/Search/Result.cshtml?searchCriteria=" + searchValue);
}
}
And that doesn't work. What whould be the alternative to Response.Redirect in mvc4, which still allows me to keep the searchCriteria to be read with Request.Querystring at the Result.cshtml page.
You should be definetly doing this in your controller, making it return an ActionResult and returning a RedirectResult, i.e.:
public ActionResult Search(string searchCriteria) {
return Redirect("~/Views/Search/Result.cshtml?searchCriteria="+searchCriteria);
}
Btw, I'd also say don't use neither of the Request stuff (or even Redirects), but actions with parameters that MVC will bind automagically from POST or GET parameters. E.g, "www.something.com/search?searchCriteria=hello" will automatically bind the searchCriteria parameter to the Action handling /search. Or, "www.something.com/search/hello" will bind to the parameter defined into your Routing config.
A simple example would be something like this:
Index.cshtml:
#using (Html.BeginForm("Results", "Search", FormMethod.Get))
{
#Html.TextBox("searchCriteria")
<input type="submit" value='Search' />
}
Then the controller:
public class SearchController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Results(string searchCriteria)
{
var model = // ... filter using searchCriteria
return View(model);
}
}
model could be of type ResultsViewModel, which would encase everything you need to display the results. This way, your search is setup in a RESTful way - meaning it behaves consistently each time.
Related
I return the View from my controller start_run1 to a view start_run1
public ActionResult start_run1(int? id) { ....
return(View);
}
I get 404 page not found, even if I try using absolute paths etc etc.
if I add the code it finds the page but model data or ViewBag data doesn't get passed.
public IActionResult start_run1()
{
return View();
}
I have another similar controller function which works without doing this.
Any ideas, Ive tried everything.
I'm having a real problem trying to articulate this seemly simple problem.
I have a single view that contains a FORM with a few search fields at the top of the view and the results of that search get shown on the same view after submitting the form.
I have a single HTTPGET controller method that takes the form fields as parameters and IF it was submitted by a user it will pass the model back to the view with the results to be shown and pre-populate the search form with what they filled out.
How can I tell if the page was loaded with default parameters vs. someone actually submitted the form.
What's the best way to accomplish this?
If I am understanding your question correctly then I think you need to consider the HttpGet attribute:
https://msdn.microsoft.com/en-us/library/system.web.mvc.httpgetattribute(v=vs.118).aspx
and the HttpPost attribute:
https://msdn.microsoft.com/en-us/library/system.web.mvc.httppostattribute(v=vs.118).aspx
Lets say you have a create method. The Http method would look like this:
[HttpGet]
public ActionResult Create()
{
}
and the post method would look like this:
[HttpPost]
public ActionResult Create(Person p)
{
//Logic to insert p into database. Could call an application service/repository to do this
}
RedirectToAction solve the problem.
you can go back to the get method after submited data and populate the view with default values
[HttpGet]
public ActionResult Create()
{
// fill model to default data
return view(model);
}
[HttpPost]
public ActionResult Create(Person p)
{
//do your stuff save data
return RedirectToAction("Create");
}
or
[HttpPost]
public ActionResult Create(Person p)
{
if(...)
{
//do your stuff any logic
return RedirectToAction("Create");
}
//do your stuff
return view(...);
}
I'm doing a simple RedirectAction in my controler and in this new Controller i'm calling the new View, however in the Browser the View is not changing, i can see in the cshtml the code getting there, but i don't know what i'm missing.
public ActionResult ExecuteBreakdown(string param1)
{
return RedirectToAction("ShowMatrix", "BreakdownMatrix", new { param = param1 });
}
public ActionResult ShowMatrix(string param1 )
{
...lots of code
return View("ShowMatrix", priceMatrix);
}
I'm not being redirect to the ShowMatrix View.
You need to return something like a PartialView in your redirect to action call. However, the call needs to be scoped to a parent view that can display the partial properly. You can't just load what you send over like that.
Inside my controller i'm returning a View which works fine except that the Url is not what I expected because it's replacing with the name of the action method it's in.
http://hostname/Controller/SubmitTicket
instead of
http://hostname/Controller/Detail
And I can't do a redirect to action in this case.
public ActionResult Detail()
{
return View();
}
public ActionResult SubmitTicket()
{
return View("Detail");
}
<h2>Detail</h2>
<% using (Html.BeginForm("SubmitTicket", "Home"))
{ %>
<input id="Submit1" type="submit" value="submit" />
<% } %>
In MVC the URL used will always be based on the Action.
I think what is happening is you are doing a POST to http://hostname/Controller/SubmitTicket, and then returning the Detail view. In this case the URL will be the URL you submitted to.
Hence if you want a different URL, one option is you need to change the name of your SubmitTicket action or define a different route for it. But I don't think that will solve your problem.
If you are doing a post, and want to show the user a detail page after the post, use the Post/Redirect/Get pattern.
public ActionResult Detail()
{
return View();
}
public ActionResult SubmitTicket()
{
return RedirectToAction("Detail")
}
My controller has 2 actions:
Results()
Index()
I want to share the view named index.aspx between these 2 actions.
See my previous post for more information
When I build my link to the page, I assume I cannot send it to Index action as it is expecting a FormCollection type and hence I create a Results action
public ActionResult Results(ClientSearch data, int? page)
{
FormCollection collection = new FormCollection();
collection.Add("FNAme", data.FName);
collection.Add("Lane", data.Lane);
collection.Add("Zip", data.Zip);
collection.Add("Phone", data.Phone);
return Index(page, collection);
}
Not sure I completely understand your question, but if you want to use the same View on different ActionResults, you can:
public ActionResult One() {
// do stuff
return View("Index", myModel);
}
public ActionResult Two() {
// do stuff
return View("Index", myOtherModel); // Same View
}
Just make sure you are providing the same Type for the View (if the View needs a Type at all).
Of course you can. It's up to controller to decide how to react and what view to serve back.
Now that I've read your question to the end :)), well, you can get away with two actions of the same name. The one will be accepting GET commands (initial load of the page), the other will be serving POST requests, perform the necessary action and redirect back to the same View.
public MyController
{
[AcceptVerbs (HttpVerbs.Get)]
public ActionResult Index ()
{
return View ();
}
[AcceptVerbs (HttpVerbs.Post)]
public ActionResult Index (ClientSearch data, int? page)
{
// Process form post
return RedirectToAction ("Index");
}
}