Where is the erroneous "Id" coming from? - c#

I'm experimenting with the MVC beta 2 and have the following method in a controller:
[HttpPost]
public ActionResult Create(Issue issue) {
if(TryUpdateModel(issue, "Model", new [] {"Title", "Description"})) {
ServiceCaller.PutIssue(issue);
return RedirectToAction("Index");
}
return RedirectToAction("Create");
}
TryUpdateModel always fails and ModelState has an error under a Key called "Id" which says that "A value is required". The View contains no Id input field and I understood that my "include" argument on the TryUpdateModel should have ignored anything other than the explicitly included fields anyway. Changing the method signature to the following fixes the problem but I'd like to understand how the "Id" field is getting included in the first place.
public ActionResult Create([Bind(Exclude = "Id")]Issue issue)
For completeness, here's the View (it's a shared view in the EditorTemplates folder):
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Tracker.Processing.Model.Issue>" %>
<% using (Html.BeginForm(Model.Id > 0 ? "Edit" : "Create", "Issue", FormMethod.Post)) { %>
<p>
<%= Html.LabelFor(i => i.Title) %>
<%= Html.EditorFor(i => i.Title) %>
</p>
<p>
<%= Html.LabelFor(i => i.Description) %>
<%= Html.TextArea("Description", Model.Description, new{ style = "width: 100%;" }) %>
</p>
<p>
<input type="submit" value="Save" />
</p>
<% } %>
Any ideas?

Its attempting to match the Id in the route to the one on your object
as the one in the route is null and the Id on your object is not nullable you get the error
Irritating isn't it

Related

Problems with Html.BeginForm() asp.net MVC 2

I have a SearchController.cs in my controller folder, it has an Action named Index. My search folder has a view named Index
The following code is in my /Controller/SearchController
private TEAM2BooksDBEntities _db = new TEAM2BooksDBEntities();
[HttpPost]
public ActionResult Index(string SearchFor)
{
var query = _db.Books.Where(em => em.title.Contains(SearchFor)).ToList();
return View(query);
}
The following code is in my /Home/Index
<% using(Html.BeginForm("Index","Search")){ %>
<%= Html.TextBox("SearchFor") %>
<input type="submit" value="Submit" />
<% }%>
But no matter what I do when I hit the submit button it just reloads the current page. I want it to send the contents of the "SearchFor" box as a parameter to the Index action in the Search controller. How can I fix this?
I also suggest try to use this.
<% using(Html.BeginForm("Index","Search",FormMethod.Post)){ %>
<%= Html.TextBox("SearchFor") %>
<input type="submit" value="Submit" />
<% }%>
Try this inside your action :
string SearchFor= Request.Form["SearchFor"];

View loop value doesn't change after first value

I have an ICollection<String> being passed to my view and I do a foreach to load a partial view. It loops through the correct number of times, however, the value it passes is the same and I know in the model that this is not the case.
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View("Index", new List<String>());
}
[HttpPost]
public ActionResult Index(List<String> txtValue)
{
return View("Index", txtValue);
}
}
View:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<List<String>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Home
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
//Deletes the div the Control is in.
$(".delete").live("click", function () {
$(this).parent().remove();
});
//Adds the TextBoxes to divControls
function AddTextBox(Value) {
var elements = "<div><input name=\"txtValue\" type=\"text\" /><input type=\"button\" class=\"delete\" value=\"-\" /><br/></div>";
$("#divControls").append(elements);
}
</script>
<h2>Controls!!!</h2>
<input id="btnAdd" type="button" name="Refresh" value="+" onclick="AddTextBox()" />
<% using (Html.BeginForm())
{ %>
<input id="btnsubmit" type="submit" name="Submit" onclick="Submit" />
<div id="divControls">
<%
foreach (var text in this.Model)
{ %>
<%=Html.TextBox("txtValue", text, new { id = "Value", name = "txtValue" })%>
<% Html.RenderPartial("TextControl", text);
}
%>
</div>
<%
}
%>
</asp:Content>
TextControl.ascx
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<String>" %>
<div>
<%=Html.TextBox("txtValue", this.Model, new { id = "Value", name = "txtValue" }) %>
<input id="btn" type="button" class="delete" value="-" /><br/>
</div>
The values of the model passed from the controller to the view are correct, even when passed to the user control "TextControl" the value is correct, but when the Textbox displays they are all just the first value of the original model passed in.
Ex.
Model as List<String> { "1", "2", "3", "4" }
passed to the view, will iterate through each one correctly, passing the correct string to "TextControl" to create a Html.TextBox("name", this.Model). Everything on the debugging side appears correct, however, when it finishes all the textboxes are "1" (or the first value in the list).
Here's a link to my exact code: http://www.sendspace.com/file/sypl1u
Note:
I came up with a solution of just using <input type="text" name="txtValue" value="<%= this.Model %>" /> instead.
Potential issue: you are using ElementAt which is LINQ method that fave special behavior for IList argument, but you are passing in txtValue as result of some query. In this case ElementAt may case sequence to be enumerated multiple times and may even fail if sequence can't be re-enumerated.
Consider simple foreach on the collection instead:
foreach (var text in Model)
{
Html.RenderPartial("TextControl", text);
}

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

Asp.Net MVC EditorTemplate Model is lost after Post

I have a controller with two simple Methods:
UserController Methods:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Details(string id)
{
User user = UserRepo.UserByID(id);
return View(user);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Details(User user)
{
return View(user);
}
Then there is one simple view for displaying the details:
<% using (Html.BeginForm("Details", "User", FormMethod.Post))
{%>
<fieldset>
<legend>Userinfo</legend>
<%= Html.EditorFor(m => m.Name, "LabelTextBoxValidation")%>
<%= Html.EditorFor(m => m.Email, "LabelTextBoxValidation")%>
<%= Html.EditorFor(m => m.Telephone, "LabelTextBoxValidation")%>
</fieldset>
<input type="submit" id="btnChange" value="Change" />
<% } %>
As you can see, I use an editor template "LabelTextBoxValidation":
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<%= Html.Label("") %>
<%= Html.TextBox(Model,Model)%>
<%= Html.ValidationMessage("")%>
Showing user information is no problem. The view renders perfectly user details.
When I submit the form, the object user is lost. I debugged on the row "return View(User);" in the Post Details method, the user object is filled with nullable values. If I dont use the editor template, the user object is filled with correct data. So there has to be something wrong with the editor template, but can't figure out what it is. Suggestions?
I would re-architect a little bit - change your LabelTextBoxValidation editor to an Html helper, and then make one EditorTemplate for your data model. That way you could do something like this:
<% using (Html.BeginForm("Details", "User", FormMethod.Post))
{%>
<fieldset>
<legend>Userinfo</legend>
<% Html.EditorFor(m => m); %>
</fieldset>
<input type="submit" id="btnChange" value="Change" />
<% } %>
And your editor template would be something like:
<%= Html.ValidatedTextBoxFor(m => m.Name); %>
<%= Html.ValidatedTextBoxFor(m => m.Email); %>
<%= Html.ValidatedTextBoxFor(m => m.Telephone); %>
where ValidatedTextBoxFor is your new html helper. To make that, it would be fairly easy:
public static MvcHtmlString ValidatedTextBoxFor<T>(this HtmlHelper helper, Expression thingy)
{
// Some pseudo code, Visual Studio isn't in front of me right now
return helper.LabelFor(thingy) + helper.TextBoxFor(thingy) + helper.ValidationMessageFor(thingy);
}
That should set the names of the form fields right I believe, as that seems like the source of the problem.
EDIT: Here is the code that should help you out:
public static MvcHtmlString ValidatedTextBoxFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
return MvcHtmlString.Create(
html.LabelFor(expression).ToString() +
html.TextBoxFor(expression).ToString() +
html.ValidationMessageFor(expression).ToString()
);
}

ASP.NET MVC Html.BeginForm Problem

I have a partial view:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DomainModel.Entities.Product>" %>
<div class="item">
<h3><%= Model.Name %></h3>
<%= Model.Description %>
<% using (Html.BeginForm("AddToCart", "Cart")) { %>
<%= Html.Hidden("ProductID") %>
<%= Html.Hidden("returnUrl", ViewContext.HttpContext.Request.Url.PathAndQuery) %>
<input type="submit" value="+ Add to cart" />
<% } %>
<h4><%= Model.Price.ToString("c")%></h4>
</div>
And here is the html that gets rendered:
<div class="item">
<h3>Kayak</h3>
A boat for one person
<form action="" method="post">
<input id="ProductID" name="ProductID" type="hidden" value="1" />
<input id="returnUrl" name="returnUrl" type="hidden" value="/" />
<input type="submit" value="+ Add to cart" />
</form>
<h4>$275.00</h4>
</div>
Nothing happens when the submit button is clicked and I am pretty sure it's because the form action attribute has no value. Shouldn't BeginForm(action, controller) take care of rendering out the form action? What am I doing wrong?
EDIT
Code from CartController AddToCart action:
public RedirectToRouteResult AddToCart(Cart cart, int productID, string returnUrl)
{
Product product = productsRepository.Products.FirstOrDefault(p => p.ProductID == productID);
cart.AddItem(product, 1);
return RedirectToAction("Index", new { returnUrl });
}
EDIT 2
The view that renders the partial:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% foreach (var product in Model) { %>
<% Html.RenderPartial("ProductSummary", product); %>
<% } %>
<div class="pager">
Page:
<%=Html.PageLinks((int)ViewData["CurrentPage"],
(int)ViewData["TotalPages"],
x => Url.Action("List", new { page = x, category = ViewData["CurrentCategory"] })) %>
</div>
</asp:Content>
EDIT 3
Routes:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
null, // don't need a name
"", // matches the root URL, i.e. ~/
new { controller = "Products", action = "List", category = (string)null, page = 1 } //Defaults
);
routes.MapRoute(
null, // don't need name
"Page{page}", // URL pattern, e.g. ~/Page683
new { controller = "Products", action = "List", category = (string)null }, // defaults
new { page = #"\d+" } // constraints: page must be numerical
);
routes.MapRoute(null,
"{category}",
new { controller = "Products", action = "List", page = 1 });
routes.MapRoute(null,
"{category}/Page{page}",
new { controller = "Products", action = "List" },
new { page = #"\d+" } // constraints: page must be numerical
);
}
It looks like you don't have a default route set up. BeginForm uses UrlHelper.GenerateUrl to match up the action/controller names to your route collection. So if you don't have a route that maps to AddToCart, then it can't generate a URL for it. Try adding this to the bottom of your routes:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Products", action = "List", id = "" }
);
This is from the main application example used in Steven Sanderson's excellent 'Pro ASP MVC Framework' book.
Funnily enough I made exactly the same mistake and omitted the final .MapRoute call given in the listing on page 130.
routes.MapRoute("Default", "controller}/{action}"
It was Johnny G's answer to this post that helped me to find my mistake as well.
Nice one Johnny!

Categories