【问题标题】:Passing Query String Values to a View Model in Asp.net MVC App将查询字符串值传递给 Asp.net MVC 应用程序中的视图模型
【发布时间】:2017-09-12 10:35:32
【问题描述】:

如何将查询字符串值传递给在 ASP.net MVC 应用程序中具有默认值的视图模型参数?

我试过了,但没有成功;

public ActionResult Index(MyAnotherVM filter){
  // filter.p doesn't set passed value and it equals to 1
}

public class MyAnotherVM {
 public int p { get { return 1; } set { } }
 // or
 public int p=1;
}

【问题讨论】:

标签: asp.net asp.net-mvc


【解决方案1】:

你可以使用 TempData

[HttpPost]
public ActionResult FillStudent(Student student1)
{
    TempData["student"]= new Student();
    return RedirectToAction("GetStudent","Student");
    }

[HttpGet]
public ActionResult GetStudent(Student passedStd)
{
    Student std=(Student)TempData["student"];
    return View();
}

方法二:

使用查询字符串数据传递

或者你可以在查询字符串的帮助下构建它,例如

return RedirectToAction("GetStudent","Student", new {Name="John",             Class="clsz"});

这将生成一个 GET 请求,例如

Student/GetStudent?Name=John & Class=clsz

但请确保您有 [HttpGet],因为 RedirectToAction 会发出 GET Request(302)

【讨论】:

    猜你喜欢
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    相关资源
    最近更新 更多