Reputation: 46
I'm running a couple classic ASP web sites on a Windows 2008 R2 server (IIS 7.5), and I added a simple MVC3 eCommerce site. The issue is when I try to add an item to the shopping cart from the product page using a form. Using the Razor syntax, I have the following code:
@using (Html.BeginForm("Add", "Cart", FormMethod.Post, new { }))
{
@Html.HiddenFor(m => m.Id)
@Html.TextBoxFor(m => m.Quantity, new { size = "1" })
<input type="submit" value="Add to Cart"/>
}
public class AddToCartViewModel
{
public string Id { get; set; }
public int Quantity { get; set; }
}
[HttpPost]
public ActionResult Add(AddToCartViewModel cartItem)
{
// Code that adds the item to the cart
// Go back to the product page
return RedirectToAction("Model", "Product", new { id = cartItem.Id });
}
The problem is that the ModelId and Quantity are not being populated with the data in the form. The cartItem is not null, and this action is being called. This code works perfectly when run through Visual Studio 2010, so I am guessing that it is server and/or IIS configuration setting. Oddly enough, I have another page for the shopping cart that allows users to update the quantities of items in their cart which works awesomely. That page is more complicated, as it uses a list in the view model so I can't see why my simple request fails.
Using an ActionLink (with a hardcoded quantity) instead of a form works.
@Html.ActionLink("Add to cart", "Add", "Cart", new { Id = Model.Id, Quantity = 1}, new {})
I tried changing the signature of the action method to take a string and an int, but an exception is thrown because the quantity is null.
Grasping at straws, I ran the following to make sure .NET 4.0 was registered, and it seems fine. I have 2.0.50727.0, and the x32 and x64 versions of 4.0.30319.0
Bin deploying the application as per Phil Haack's instructions did not work either
I'm out of ideas since everything works in Visual Studio, and I can't find other posts with this same issue. Am I just missing something simple? Also, I just noticed that the logging in via the account controller doesn't work on the server either. There are no errors or validation issues shown. I'm using the default membership provider, but the web.config is configured to use my own database. The connection works, as I can register a new user and that new user is logged in on successful registration.
Upvotes: 2
Views: 643
Reputation: 22485
[Edit]
ok, updated answer now that you've updated your question. I think the issue may well be down to the fact that you don't initialise the model in your initial get request for the Add action. try adding the HttpGet
and HttpPost
as per below:
[HttpGet]
public ActionResult Add()
{
var viewModel = new AddToCartViewModel()
{
Id = "myid",
Quantity = 0
};
return View(viewModel);
}
[HttpPost]
public ActionResult Add(AddToCartViewModel cartItem)
{
// Code that adds the item to the cart
// Go back to the product page
return RedirectToAction("Model", "Product", new { id = cartItem.Id });
}
I'm sure this will work now.
Upvotes: 1
Reputation: 32768
Use strongly typed html helpers
@Html.HiddenFor(m => m.Id)
@Html.TextBoxFor(m => m.Quantity, new { size = "1" })
and make sure you have marked the action with [HttpPost].
Upvotes: 0