如何传递变量的值从得到后MVC

本文关键字:MVC 何传递 变量 | 更新日期: 2023-09-27 18:05:26

获取方法:

public ActionResult Add(string id)
{}

我想读取post

方法中变量"id"的值
[HttpPost]
public ActionResult Add(List<class_of_data> clazz)
{}

如何传递变量的值从得到后MVC

基本上你可以这样做:

public ActionResult Add(string id)
{
    ViewBag.ModelId = id;
    return View();
}
[HttpPost]
public ActionResult Add(List <class_of_data> classList, string id)
{
// you can set int id if you need it as an number since MVC will convert it.
// Also keep in mind that class in a reserved keyword. Changed it to classList 
// DO SOME STUFF
}

视图应该是这样的:

@using(Html.BeginForm())
{
// some code
<input type='hidden' name='id' value='@ViewBag.ModelId' />
// some code
}

如果您正在提交表单并希望丢失模型绑定或没有模型绑定,那么此解决方案可能对您有用。

[HttpPost]
    public ActionResult FormSubmit(FormCollection form)
    {
        string id= form["id"].ToString();
  return RedirectToAction("Index");
        }
    }

你的视图应该是这样的

<input type="textbox" name="id" />
<input type="submit" Value="Submit Form" />