C# 如何将dropdownbox的选定值从mvc视图传递到控制器操作方法?
我的视图中有一个下拉框,由sql数据库表填充。在下拉列表中,我有不同的值,我想将单击按钮时下拉列表的选定值传递给控制器操作方法。下面是下拉列表的填充方式C# 如何将dropdownbox的选定值从mvc视图传递到控制器操作方法?,c#,ajax,asp.net-mvc,C#,Ajax,Asp.net Mvc,我的视图中有一个下拉框,由sql数据库表填充。在下拉列表中,我有不同的值,我想将单击按钮时下拉列表的选定值传递给控制器操作方法。下面是下拉列表的填充方式 public ViewResult ProductDetails(int productId) { Product product = repository.Products .Include(p => p.Reviews) .First
public ViewResult ProductDetails(int productId)
{
Product product = repository.Products
.Include(p => p.Reviews)
.FirstOrDefault(p => p.ProductID == productId);
List<string> available = new List<string>();
available.AddRange(product.AvailableSizes.Split(',').ToList());
ViewData["AV"] = new SelectList(available);
return View(product);
}
我的控制器操作方法是:
public ActionResult AddToCart(Cart cart, int productId, string returnUrl, string size)
{
Product product = repository.Products
.FirstOrDefault(p => p.ProductID == productId);
if (product != null)
{
cart.AddItem(product, 1,size); //no overload for method 'AddItem' takes 3 argument, Error!
}
return RedirectToAction("Index", new { returnUrl });
}
最后,cart类如下所示:
public void AddItem (Product product, int quantity)
{
CartLine line = lineCollection
.Where(p => p.Product.ProductID == product.ProductID)
.FirstOrDefault();
if (line == null)
{
lineCollection.Add(new CartLine { Product = product, Quantity = quantity });
}
else
{
line.Quantity += quantity;
}
}
我试过了,但是运气不好,知道吗
为Rion Williams爵士编辑:
下面是整个购物车类的详细信息
public class Cart
{
private List<CartLine> lineCollection = new List<CartLine>();
public void AddItem (Product product, int quantity)
{
CartLine line = lineCollection
.Where(p => p.Product.ProductID == product.ProductID)
.FirstOrDefault();
if (line == null)
{
lineCollection.Add(new CartLine { Product = product, Quantity = quantity });
}
else
{
line.Quantity += quantity;
}
}
public void RemoveLine (Product product)
{
lineCollection.RemoveAll(l => l.Product.ProductID == product.ProductID);
}
public decimal ComputeTotalValue()
{
return lineCollection.Sum(e => e.Product.ProductPrice * e.Quantity);
}
public void Clear()
{
lineCollection.Clear();
}
public IEnumerable<CartLine> Lines
{
get { return lineCollection; }
}
public class CartLine
{
public Product Product { get; set; }
public int Quantity { get; set; }
}
}
公共类购物车
{
私有列表lineCollection=新列表();
公共无效附加项(产品、整数数量)
{
CartLine line=lineCollection
.Where(p=>p.Product.ProductID==Product.ProductID)
.FirstOrDefault();
如果(行==null)
{
添加(新的CartLine{Product=Product,Quantity=Quantity});
}
其他的
{
行.数量+=数量;
}
}
公共空隙清除线(产品)
{
lineCollection.RemoveAll(l=>l.Product.ProductID==Product.ProductID);
}
公共十进制计算总价值()
{
returnlinecollection.Sum(e=>e.Product.ProductPrice*e.Quantity);
}
公共空间清除()
{
lineCollection.Clear();
}
公共可数线
{
获取{return lineCollection;}
}
公共类CartLine
{
公共产品产品{get;set;}
公共整数数量{get;set;}
}
}
如果视图包含以下元素:
@Html.DropDownList("AV")
这意味着,无论您如何将其发布到您的服务器,都需要一个名为AV
的显式参数或绑定到名为AV
的模型上的属性:
public ActionResult AddToCart(Cart cart, int productId, string returnUrl, string size, string AV)
{
// AV should be populated as long as you are properly serializing
// the <form> with the "AV" element in it and it should store the
// selected value
}
单击“提交”按钮时,这将将
的值发布到AddToCart()
操作,并将AV
属性绑定到其选定值:
// The [HttpPost] decorator allows this to accept POST requests
[HttpPost]
public void AddToCart(string AV)
{
// You should be able to see your selected value here (example)
var selected = AV;
}
AJAX示例
AJAX示例的工作原理与前面的方法非常相似,但在
本身的实际发布方式上有所不同。如果您使用的是jQuery,您可以连接一个事件来捕获submit
事件,忽略它并通过AJAX手动POST
发布内容:
<form action='@Url.Action("AddToCart","YourController")' method='post'>
@Html.DropDownList("AV")
<input type='submit' value='Add to Cart' />
</form>
<script>
$(function(){
$('form').submit(function(e){
// Ignore any default behavior / submission
e.preventDefault();
// Make an AJAX post to your action (short-hand)
$.post('@Url.Action("AddToCart","YourController")', $(this).serialize(), function(){
alert('Your value was posted successfully!');
});
});
});
</script>
如果视图包含以下元素:
@Html.DropDownList("AV")
这意味着,无论您如何将其发布到您的服务器,都需要一个名为AV
的显式参数或绑定到名为AV
的模型上的属性:
public ActionResult AddToCart(Cart cart, int productId, string returnUrl, string size, string AV)
{
// AV should be populated as long as you are properly serializing
// the <form> with the "AV" element in it and it should store the
// selected value
}
单击“提交”按钮时,这将将
的值发布到AddToCart()
操作,并将AV
属性绑定到其选定值:
// The [HttpPost] decorator allows this to accept POST requests
[HttpPost]
public void AddToCart(string AV)
{
// You should be able to see your selected value here (example)
var selected = AV;
}
AJAX示例
AJAX示例的工作原理与前面的方法非常相似,但在
本身的实际发布方式上有所不同。如果您使用的是jQuery,您可以连接一个事件来捕获submit
事件,忽略它并通过AJAX手动POST
发布内容:
<form action='@Url.Action("AddToCart","YourController")' method='post'>
@Html.DropDownList("AV")
<input type='submit' value='Add to Cart' />
</form>
<script>
$(function(){
$('form').submit(function(e){
// Ignore any default behavior / submission
e.preventDefault();
// Make an AJAX post to your action (short-hand)
$.post('@Url.Action("AddToCart","YourController")', $(this).serialize(), function(){
alert('Your value was posted successfully!');
});
});
});
</script>
Rion对此表示感谢,但我对mvc还不太熟悉,有没有可能说清楚一点?我没有一个名为AV的属性,也没有一个参数。您建议如何?简单地“检查”您的值是否已过帐最简单的方法是,您可以在add to cart方法中添加一行,以检查AV
值,如下所示:var selected=Request[“AV”]代码>。您可以使用调试器查看该值是否正确填充,然后根据需要使用它。或者,您可以使用我前面提到的方法,实际向名为AV
的操作添加一个参数,MVC将自行处理绑定值的操作。Williams爵士,您可以再次查看我的问题吗?我在操作方法中传递的参数“size”显示错误,错误写在问题中。再次感谢您您可以看到cart line集合,这是一个私有方法,问题会相应更新,以获取更多信息。使用建议的方法,值不会发布到控制器,我通过警报(大小)单击按钮后获得值,但未发布到控制器的值显示为空,有什么建议吗?Rion谢谢你,但我对mvc还不太熟悉,有没有可能说清楚一点?我没有一个名为AV的属性,也没有一个参数。您建议如何?简单地“检查”您的值是否已过帐最简单的方法是,您可以在add to cart方法中添加一行,以检查AV
值,如下所示:var selected=Request[“AV”]代码>。您可以使用调试器查看该值是否正确填充,然后根据需要使用它。或者,您可以使用我前面提到的方法,实际向名为AV
的操作添加一个参数,MVC将自行处理绑定值的操作。Williams爵士,您可以再次查看我的问题吗?我在操作方法中传递的参数“size”显示错误,错误写在问题中。再次感谢您您可以看到cart line集合,这是一个私有方法,问题会相应地更新,以获取更多信息。使用建议的方法,值不会发布到控制器,我通过警报(大小)单击按钮后获得值,但未发布到控制器的值显示为空,有任何建议吗?