简体   繁体   中英

Returning view with model and query string

I am trying to return to view from a controller that has both querystring and model

return View("BillingReport.aspx/?report=" + fc["report_selector"], bilDet);

but this gives me a runtime error of page not found as it appends .aspx etc at the end of the url.

RedirectToAction() doesnt have an option to do it.
Is there a way to do it or does mvc3 limit us to using either a query string or a model

MVC does not support what you are looking for,

But I dont understand why do you want to Redirect To a URL with ModelValues.

Any redirection is a GET request, so you can construct the model and return View from that action.

View() expects a view name and model associated with it.

Redirect() or RedirectToAction() are used to redirect the url to another controller/action. So you can not pass a model.Even if you will try to pass model it will append model properties as querystring parameters.

Here is a reason why you would want use the model and the querystring: the querystring allows you to give user way to save URL with state information. The model allows you to pass a lot of non-flattened data around. So here is I think how to do this in MVC 5 (maybe does not work for older versions, but probably does):

Use 2 actions rather than 1 for the view. use first one to set querystring via RedirectToAction. Use second action to return model to the view. And you pass the model from the first action to the second action via session state. Here is example code:

public ActionResult Index(string email){
    Session["stuff"]=Load(email);
    return RedirectToAction("View1action", new { email = email, color = "red" });
}

public ActionResult View1action(string email){
    return View("View1",(StuffClass)Session["stuff"]);
}

I agree with Manas's answer and if I were you I would consider changing the design if possible.
As a side note, the following technique is possible:

TempData["bilDet"] = bilDet;
return RedirectToAction(....);   // your controller, action etc.

On the action you can then retrieve your TempData. TempData will automatically be removed.

But also check out: ASP.NET MVC - TempData - Good or bad practice

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM