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.
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.