简体   繁体   English

在 asp.net mvc 中从视图重定向到不同的视图

[英]Redirect from view to different view in asp.net mvc

I have a problem transfering data from one view to another via the controler actions.我在通过控制器操作将数据从一个视图传输到另一个视图时遇到问题。 I the first view is a grid.mvc-Grid displayed.我的第一个视图是一个 grid.mvc-Grid 显示。 By select on row of the grid I get the ID for that object.通过选择网格的行,我得到了该对象的 ID。 by transfering this to an action in the controler I try to filter the data.通过将其转移到控制器中的操作,我尝试过滤数据。 That works fine.这很好用。

Here is the filter:这是过滤器:

[HttpGet]
public ActionResult PersonenById(int id)
{
    var personen = new ObservableCollection<Person>();
    //Getting the data here :-)

    foreach (DataRow r in access.Rows)
    {
        Person p = new Person();
        //do some stuff 
        personen.Add(p);
    }

    //return PartialView("Personen", personen); //does not work
    TempData["personen"] = personen;
    return RedirectToAction("Personen"); // redirect to another view
}

In method II the view is filled:在方法二中,视图被填充:

public ActionResult Personen()
{
    var persons = new ObservableCollection<Person>();
    if (TempData["Persons"] == null)
    {

     }
  return View(persons); //Works perfect
}   
else
{
    persons = (ObservableCollection<Person>) TempData["Persons"];
    return View(persons);//does not redirect to that View
}

} }

(Sorry for the strange formating. :-)) (抱歉奇怪的格式。:-))

Is there any different way to send data from a view to another?有什么不同的方法可以将数据从一个视图发送到另一个视图吗? I tried: return partial;我试过:返回部分; return View("Persons",persons);返回视图(“人”,人); and a lot other stuff.还有很多其他的东西。

Should work like this:应该像这样工作:

return RedirectToAction("Personen", model);

Also, the "Personen" action should have the model as an argument, like this:此外,“Personen”操作应该将模型作为参数,如下所示:

public ActionResult Personen(Person model) ...

LE: I have also noticed you have tried to send the data through the TempData object. LE:我还注意到您尝试通过 TempData 对象发送数据。 Make sure the indexed object's name is the same (eg TempData["person"] everywhere)确保索引对象的名称相同(例如TempData["person"]无处不在)

Hope it answers your question.希望它能回答你的问题。

You can redirect in a .cshtml view.您可以在 .cshtml 视图中重定向。

Eg:例如:

    Context.Response.StatusCode = 403;
    Context.Response.Redirect(
        $"{Context.Request.PathBase}/Error/403", false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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