Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - how to reload same view after posting data?

I have a view that contains a Google map with a default zoom level and position set on a country. The default position to show on the map comes from DB and what I do is that I have a [HttpGet]Index action that fetches this information, sends it to the view and then in the view I use JS to draw the map (just usual google map stuff to draw a simple map).

Then just above the map div, I show a textbox and a search button. So the user types in an address and presses the search button, I post the value of textbox to a [HttpPost]Index action. Now I want to pass this value to [HttpGet]Index action so that it goes and fetches data from DB for that address, send it to the Index view so that the map is redrawn with that particular address set as the center of the map and showing the address in the textbox as well.

Any suggestions on how can I do this?

The way I have tried (and it didn't work) is to call the Get version of Index action and pass the address to it by using TempData dictionary. But all i see is an empty/blank page.

Here is how my HttpGet Index action looks:

[HttpGet]
public async Task<ActionResult> Index()
{
    // I fetch the info from DB here put it inside ViewBag and call the view    
            return View();
}

This loads the info correctly and the map is drawn as it should when the URL controller/Index is hit first time in the browser.

The markup for the textbox and search button is:

<form class="form" id="searchForm" action="@Url.Action("Index", "Default")" method="post">
<div class="form-group">
    <div class="row">
        <div class="col-xs-9 col-sm-9 col-md-10">
           <label for="address" class="sr-only">Search for address</label>
           <input type="text" class="form-control" id="address" placeholder="Search for address" name="address">
         </div>
    <div class="col-xs-3 col-sm-3 col-md-2">
    <input type="submit" class="btn btn-block btn-success" value="Search" />
    </div>
   </div>
</div>
</form>

and finally here is the HttpPost Index action:

[HttpPost]
public void Index(String address)
{
   TempData["address"] = address;
   Index();
}

So when I submit the form, I want that it gets posted to the HttpPost Index action and then it passes the value to the HttpGet version of it which will eventually get data from DB and get the view with updated map. But all I see is a blank page.

Any help?

like image 495
Syed Avatar asked Apr 16 '15 12:04

Syed


People also ask

Can I return ActionResult Instead of view results?

First to Understand---ActionResult and ViewResult are basically a return type for an method(i.e Action in MVC). Second The Difference is --- ActionResult can return any type of Result Whereas ViewResult can return result type of View Only.

Can we return partial view in MVC?

In ASP.NET Core MVC, a controller's ViewResult is capable of returning either a view or a partial view. In Razor Pages, a PageModel can return a partial view represented as a PartialViewResult object. Referencing and rendering partial views is described in the Reference a partial view section.

Can MVC have multiple views?

-Model View Controller (MVC): a single model is mapped to multiple views: the different synchronized channels. Multiple controllers act on and transform the underlying model by interacting via one or more views.


1 Answers

like that

[HttpPost]
public void Index(String address)
{
   TempData["address"] = address;
   RedirectToAction("Index");
}

you see it blank because you are redirecting RedirectToAction("Index"); and storing data in TempData and u r not using it in the index Get method

[HttpGet]
public async Task<ActionResult> Index()
{
    // I fetch the info from DB here put it inside ViewBag and call the view   
    // you must check for the temp data
    if (!string.IsNullOrWhiteSpace(TempData["address"].ToString()))
    {
         ViewBag["result"] = TempData["address"];
         //and use you viewbag data in the view
    }
    return View();
}
like image 55
Sherif Ahmed Avatar answered Nov 15 '22 08:11

Sherif Ahmed