如何在MVC2中读取HttpPost中的TextArea值和模型

发布于 2024-10-27 12:54:07 字数 1568 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谁的年少不轻狂 2024-11-03 12:54:07

我认为以下应该可行:

[HttpPost]
public ActionResult ViewMyProducts(string MyTextArea)

一件有用的事情是在表单中显式调用您的操作 - 通过更改此行:

<% using (Html.BeginForm())

<% using (Html.BeginForm("ViewMyProducts","ControllerName",HttpMethod.Post))

确保提交操作将其重定向到正确的操作。

就模型而言:

如果您只是检查计数 - 您可以创建一个隐藏字段来返回“模型”中的项目数,如下所示:

<%: Html.Hidden("modelCount", Model.Count());

但如果您想要整个模型 - 则需要像这样的东西:

<%: Html.Hidden("myModel", Model);

那么你可以进一步修改你的操作,看起来像这样:

ViewMyProducts(string MyTextArea, int modelCount)
{
    //...
}

或者

ViewMyProducts(string MyTextArea, IEnumerable<MyProject.MyDB.MyProducts> myModel)
{
    //...
}

虽然你可以访问控制器内部来刷新模型 - 所以如果你不需要传回整个东西,你仍然可以重新填充你的视图一个新的电话。

I would think that the following should work:

[HttpPost]
public ActionResult ViewMyProducts(string MyTextArea)

A helpful thing to do would be to explicitly call your Action in your Form - by changing this line:

<% using (Html.BeginForm())

to

<% using (Html.BeginForm("ViewMyProducts","ControllerName",HttpMethod.Post))

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:

<%: Html.Hidden("modelCount", Model.Count());

but if you want the entire Model - it would be need to be something like this:

<%: Html.Hidden("myModel", Model);

then you could further modify your Action to look something like this:

ViewMyProducts(string MyTextArea, int modelCount)
{
    //...
}

or

ViewMyProducts(string MyTextArea, IEnumerable<MyProject.MyDB.MyProducts> myModel)
{
    //...
}

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.

浅暮の光 2024-11-03 12:54:07

string myTextArea - 或者您可以只检查 FormCollection (我会推荐指定的变量)。

如果您想从视图中获取模型,则还需要将其序列化才能获取值。如果是这种情况,我会将整个事物转换为一个视图模型,该视图模型要么派生自您的模型,要么具有作为您的模型的公共属性,为 MyTextArea 添加一个属性,然后为您的模型发出隐藏的输入,以适当的名称命名特性。假设您的模型保存在某个地方(数据库),我只需传递密钥(id)并从操作结果中重新水化对象。

   [HttpPost]
    public ActionResult ViewMyProducts(ViewMyProductsViewModel viewModel)
    {
        if(viewModel.MyTextArea == "something" && (IEnumerable<foo>)myModel).Count()==5))         {
          var model = repo.Get(myModel.First().Id);
          // do something with the model
        }
        return View(viewModel);
    }



<%@ 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>
                <input type="hidden" name="viewModel.Id" value="<%:item.id%>" />
                <%: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>

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.

   [HttpPost]
    public ActionResult ViewMyProducts(ViewMyProductsViewModel viewModel)
    {
        if(viewModel.MyTextArea == "something" && (IEnumerable<foo>)myModel).Count()==5))         {
          var model = repo.Get(myModel.First().Id);
          // do something with the model
        }
        return View(viewModel);
    }



<%@ 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>
                <input type="hidden" name="viewModel.Id" value="<%:item.id%>" />
                <%: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>
墨小墨 2024-11-03 12:54:07

首先,您将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文