如何在MVC2中读取HttpPost中的TextArea值和模型
我是 MVC2 的新手,很抱歉问了这个愚蠢的问题。我寻找好的答案,但找不到。所以我的问题是:
我有视图:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MyProject.MyDB.MyProducts>>" %>
<%@ Import Namespace="MyProject.MyDB" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Content" runat="server">
<% using (Html.BeginForm())
{%>
<table>
<%
foreach (var item in Model)
{%>
<tr>
<td>
<%:item.name%>
</td>
<td>
<%:String.Format("{0:g}", item.date)%>
</td>
</tr>
<% } %>
</table>
<div>
<%:Html.TextArea("MyTextArea")%>
</div>
<p>
<input type="submit" value="Send" />
</p>
<% } %>
</asp:Content>
我的控制器是:
[HttpGet]
public ActionResult ViewMyProducts(int id)
{
List<MyProducts> myModel = GetMyProducts(id);
return View(myModel);
}
[HttpPost]
public ActionResult ViewMyProducts(/*???What should I put here???*/)
{
if(/*I want "MyTextArea" value here*/ == something && myModel/*from view*/.count==5}
{
//do something
}
return View(myModel);
}
所以,在 HttpPost 中,我需要视图中的 myModel 和视图中的“MyTextArea”值。我怎样才能得到它们?我将不胜感激任何帮助。
I'm new in MVC2, so sorry for stupid question. I looked for nice answer, but can't find it. So my question is:
I have view:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MyProject.MyDB.MyProducts>>" %>
<%@ Import Namespace="MyProject.MyDB" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Content" runat="server">
<% using (Html.BeginForm())
{%>
<table>
<%
foreach (var item in Model)
{%>
<tr>
<td>
<%:item.name%>
</td>
<td>
<%:String.Format("{0:g}", item.date)%>
</td>
</tr>
<% } %>
</table>
<div>
<%:Html.TextArea("MyTextArea")%>
</div>
<p>
<input type="submit" value="Send" />
</p>
<% } %>
</asp:Content>
My controller is:
[HttpGet]
public ActionResult ViewMyProducts(int id)
{
List<MyProducts> myModel = GetMyProducts(id);
return View(myModel);
}
[HttpPost]
public ActionResult ViewMyProducts(/*???What should I put here???*/)
{
if(/*I want "MyTextArea" value here*/ == something && myModel/*from view*/.count==5}
{
//do something
}
return View(myModel);
}
So, in HttpPost I need myModel from view and value of "MyTextArea" from view. How can I get them?? I'll appreciate any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为以下应该可行:
一件有用的事情是在表单中显式调用您的操作 - 通过更改此行:
以
确保提交操作将其重定向到正确的操作。
就模型而言:
如果您只是检查计数 - 您可以创建一个隐藏字段来返回“模型”中的项目数,如下所示:
但如果您想要整个模型 - 则需要像这样的东西:
那么你可以进一步修改你的操作,看起来像这样:
或者
虽然你可以访问控制器内部来刷新模型 - 所以如果你不需要传回整个东西,你仍然可以重新填充你的视图一个新的电话。
I would think that the following should work:
A helpful thing to do would be to explicitly call your Action in your Form - by changing this line:
to
to ensure that the Submit action redirects it to the right Action.
As far as the model is concerned:
If you are just checking the Count - you could make a hidden field that returns the number of items in the "Model" like such:
but if you want the entire Model - it would be need to be something like this:
then you could further modify your Action to look something like this:
or
Although you have access inside of the Controller to refresh the Model - so if you didn't need to pass back the entire thing you could still repopulate your view with a fresh call.
string myTextArea - 或者您可以只检查 FormCollection (我会推荐指定的变量)。
如果您想从视图中获取模型,则还需要将其序列化才能获取值。如果是这种情况,我会将整个事物转换为一个视图模型,该视图模型要么派生自您的模型,要么具有作为您的模型的公共属性,为 MyTextArea 添加一个属性,然后为您的模型发出隐藏的输入,以适当的名称命名特性。假设您的模型保存在某个地方(数据库),我只需传递密钥(id)并从操作结果中重新水化对象。
string myTextArea - or you could just check the FormCollection (I would recommend the named variable).
If you want to get the model back from the view, you will need to serialize it out as well in order to get back the values. If this is the case, I would convert the whole thing to a view model that either derives from your Model or has a public property that is your model, add a property for MyTextArea and then emit hidden input's for you model, named for the appropriate properties. Assuming that your model is persisted somewhere (database), I would just pass the key (id) and rehydrate the object from within the action result.
首先,您将 item.name 和 item.date 呈现为文本而不是 html 控件。因此您将无法在控制器方法参数中接收它。
First of all You are rendering item.name and item.date as a text not html control. So You won't be able to receive it in controller method parameters.