开发者

how to read TextArea value and model in HttpPost in MVC2

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:

[开发者_C百科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.


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.


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜