C# ViewModel不工作
我的表格:C# ViewModel不工作,c#,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net Mvc,Asp.net Mvc 4,Razor,我的表格: [HttpPost] public ActionResult AddToCart(int phoneListingID, string sellerSKU) { ShoppingBasket shoppingBasket = new ShoppingBasket(); BasketItem currentItem = new BasketItem { sellerID = 1, Price = 100, Quan
[HttpPost]
public ActionResult AddToCart(int phoneListingID, string sellerSKU)
{
ShoppingBasket shoppingBasket = new ShoppingBasket();
BasketItem currentItem = new BasketItem
{
sellerID = 1,
Price = 100,
Quantity = 1,
sellerSKU = "testsku"
};
shoppingBasket.AddtoBasket(currentItem, this.HttpContext);
var viewModel = new BasketViewModel
{
basketItems = ShoppingBasket.GetBasketItems(this.HttpContext),
basketTotal = ShoppingBasket.GetBasketTotal(this.HttpContext)
};
return View(viewModel);
}
@使用(Html.BeginForm(“AddToCart”,“ShoppingBasket”,new{phoneListingID=12345,sellerSKU=“test”},FormMethod.Post))
{
}
预期结果是返回my BasketViewModel页面,但是返回的视图是ShoppingBasket/AddToCart?PhoneID=xxxx&sellerSKU=xxxx
我做错了什么?您正在返回该控制器的视图,如果您希望转移到另一个视图,请尝试
返回BasketViewActionResult(viewmodel)
然后访问您的“BasketViewActionResult”
@using (Html.BeginForm("AddToCart","ShoppingBasket",new { phoneListingID = 12345, sellerSKU = "test"}, FormMethod.Post ))
{
<input type="submit" value="AddToCart" />
}
对不起,如果你没有VB,如果你愿意,我可以帮你翻译成C
编辑:
您也可以简单地更改表单的操作
Function BasketViewActionResult(model as BasketViewModel) as ActionResult
return View(model)
End Function
并在MVC中的actionresult中进行所有操作,假设您的操作如下
@using (Html.BeginForm("BasketView","ShoppingBasket",...
在此场景中,它将指向名为“MyAction”的视图。如果要将其发送到另一个视图,请按
public ActionResult MyAction()
{
return View();
}
public ActionResult MyAction()
{
return View("MyViewName");
}
如果你想通过一些模型,使它像
public ActionResult MyAction()
{
return View();
}
public ActionResult MyAction()
{
return View("MyViewName");
}
在您的代码段中,您正在返回默认视图,即“AddToCart”视图,因为您没有明确描述。使您的代码像
public ActionResult MyAction()
{
return View("MyViewName",model); // Here model is your object of model class
}
我已经编辑了你的标题。请参阅“”,其中的共识是“不,他们不应该”。