Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 访问已发布表单数据(MVC)时遇到问题 如何在action方法中访问过账表单数据并在view中显示_C#_Asp.net Mvc - Fatal编程技术网

C# 访问已发布表单数据(MVC)时遇到问题 如何在action方法中访问过账表单数据并在view中显示

C# 访问已发布表单数据(MVC)时遇到问题 如何在action方法中访问过账表单数据并在view中显示,c#,asp.net-mvc,C#,Asp.net Mvc,我正在MVC中创建一个输入向导。有两个视图,一个用于从用户获取输入,另一个用于显示输入数据 以下是模型的代码: public class Info { public int CustomerId { set; get; } public double Price { set; get; } public string name { set; get; } } } 控制器代码- public ActionResult FillCustomer()

我正在MVC中创建一个输入向导。有两个视图,一个用于从用户获取输入,另一个用于显示输入数据

以下是模型的代码:

 public class Info
  {
    public int CustomerId { set; get; }
    public double Price { set; get; }
    public string name { set; get; }
  }
}
控制器代码-

    public ActionResult FillCustomer()
    {
        return View();
    }

    public ActionResult DisplayCustomer(FormCollection frm)
    {
        Info info = new Info();

        info.name = Convert.ToString( Request.Form["name"]);
        info.Price = Convert.ToDouble(Request.Form["price"]);
        info.CustomerId = Convert.ToInt32(Request.Form["customerid"]);

        return View(info);
    }
填充客户视图的代码-

<form method="post" action="DisplayCustomer">
Name: <input type="text" id="name" /><br />
Price: <input type="text" id="price" /><br />
CustomerId: <input type="text" id="customerid" />
<br />
<input type="submit" id="btn1" />
</form>
</body>
</html>
<body>
<div>
Name is <%=Model.name %> and ID is <%=Model.CustomerId %>

<%if (Model.Price > 200)
  {%>

Greater than 200

<%}
  else %>

<%{%>Lesser than 200 

    <%} %>
    </div>
</body>
</html>

名称:
价格:
客户ID:
显示客户视图的代码-

<form method="post" action="DisplayCustomer">
Name: <input type="text" id="name" /><br />
Price: <input type="text" id="price" /><br />
CustomerId: <input type="text" id="customerid" />
<br />
<input type="submit" id="btn1" />
</form>
</body>
</html>
<body>
<div>
Name is <%=Model.name %> and ID is <%=Model.CustomerId %>

<%if (Model.Price > 200)
  {%>

Greater than 200

<%}
  else %>

<%{%>Lesser than 200 

    <%} %>
    </div>
</body>
</html>

姓名是,ID是
200)
{%>
大于200
少于200

我用调试器检查了一下,控制器没有收到发布的数据。

我想您没有在DisplayCustomer函数顶部写入属性。只需编写它,我希望它能工作

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DisplayCustomer(FormCollection frm)
{
    Info info = new Info();

    info.name = Convert.ToString( Request.Form["name"]);
    info.Price = Convert.ToDouble(Request.Form["price"]);
    info.CustomerId = Convert.ToInt32(Request.Form["customerid"]);

    return View(info);
}

表单的输入元素应具有name属性

<form method="post" action="DisplayCustomer">
Name: <input type="text" id="name" name="name" /><br />
Price: <input type="text" id="price" name="price" /><br />
CustomerId: <input type="text" id="customerid" name="customerid" />
<br />
<input type="submit" id="btn1" />
</form>

在控制器中,表单数据作为键/值对接收。键是从名称属性生成的。

您使用的是MVC,因此DisplayCustomer方法中的参数是从视图传递的模型,该视图由模型绑定器隐式创建。将类型从FormCollection更改为Info类,如下所示:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DisplayCustomer(Info model)
{
   // you can now access to Info properties
}

通过这种方式,信息对象的值会被压缩,我认为这就是您试图实现的目标。

我通过放置这些属性进行了检查。但它仍然没有得到发布的数据。