尝试在MVC中获取我的视图以传递从用户获取的两个原始值并将它们传递回控制器时遇到了很多麻烦,而视图使用的模型是完全不同的嵌套模型。 / p>
我尝试传递整个产品或ProducWithAttributes,但它在AddToCart控制器中始终显示为null。
namespace EcommerceWebsite_1.Models
{
using System;
using System.Collections.Generic;
public partial class Prod_Attribute
{
public int product_id { get; set; }
public string property_name { get; set; }
public string property_value { get; set; }
public virtual Product Product { get; set; }
}
}
namespace EcommerceWebsite_1.Models
{
using System;
using System.Collections.Generic;
public partial class Product
{
public int current_quantity { get; set; }
public int product_id { get; set; }
public string name { get; set; }
public decimal price { get; set; }
public string cat_name { get; set; }
public byte[] image { get; set; }
public Nullable<int> Merchandise_payment_order_id { get; set; }
public bool out_of_stock { get; set; }
public Nullable<int> max_amnt_per_order { get; set; }
}
}
public class ProductWithAttributes
{
public Product Prod { get; set; }
public List<Prod_Attribute> Prod_attributes { get; set; }
public int selectedQuant { get; set; }
}
public class ProductWithQuant
{
public int prod_id { get; set; }
public int selectedQuant { get; set; }
}
public ActionResult DetailsForUser(int id)
{
Product product = db.Products.Find(id);
List<Prod_Attribute> attributes_vals = db.Prod_Attribute.Where(p => p.product_id == id).OrderBy(c => c.property_name).ToList();
int quantity_selection;
if (product.max_amnt_per_order!= null)
{
quantity_selection = (int)product.max_amnt_per_order;
}
else
{
quantity_selection = product.current_quantity;
}
int[] quantity_selection_list = new int[quantity_selection];
for (int i=1; i<= quantity_selection; i++)
{
quantity_selection_list[i - 1] = i;
}
ViewBag.Quantities = quantity_selection_list;
return View(new ProductWithAttributes
{
Prod = product,
Prod_attributes = attributes_vals
});
}
@using EcommerceWebsite_1.Models
@model EcommerceWebsite_1.Controllers.ProductsController.ProductWithAttributes
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Product</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Prod.name)
</dt>
<dd>
@Html.DisplayFor(model => model.Prod.name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Prod.price)
</dt>
<dd>
@Html.DisplayFor(model => model.Prod.price)
</dd>
<dd>
@Html.DisplayFor(model => model.Prod.image)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Prod.max_amnt_per_order)
</dt>
<dd>
@Html.DisplayFor(model => model.Prod.max_amnt_per_order)
</dd>
@foreach (Prod_Attribute attr in Model.Prod_attributes)
{
<dt>
@Html.DisplayFor(model => attr.property_name)
</dt>
<dd>
@Html.DisplayFor(model => attr.property_value)
</dd>
}
<dd class="form-group">
@Html.Label("Desired Quantity: ", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.selectedQuant, new SelectList(ViewBag.Quantities), "Select Quantity...", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessage("", new { @class = "text-danger" })
</div>
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Add to Cart", "AddtoCart", new { product_id = Model.Prod.product_id, selectedQuant = Model.selectedQuant})
@Html.ActionLink("Back to List", "IndexByCategory", new { cat_name = Model.Prod.cat_name })
</p>
public ActionResult AddToCart([Bind(Include = "product_id, selectedQuan")] ProductWithQuant prod_added_To)
{
if ((System.Web.HttpContext.Current.User != null) && System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
Product product = db.Products.Find(prod_added_To.prod_id);
Added_to newAddition = new Added_to
{
product_id = prod_added_To.prod_id,
member_id = User.Identity.GetUserId(),
time_product_added = DateTime.Now,
quantity = prod_added_To.selectedQuant,
discounted_price = (decimal) CalcDiscountPrice(product)
};
db.Added_to.Add(newAddition);
db.SaveChanges();
return View(product);
}
else
{
string returnUrl = Request.Url.Host + "/Products/IndexForUser/" + prod_added_To.prod_id.ToString();
return RedirectToAction("Login", "Account", returnUrl );
}
视图的模型是一个具有另一个对象Product和对象列表Prod_attributes的对象。我试图获取用户想要购买的产品的选定数量值,然后仅将产品ID和选定数量传递回另一个控制器。或者,我可以传递整个产品对象和选定的数量。我尝试了不同的组合,但是没有任何效果,因为我不断收到最终控制器Login收到的空值。我究竟做错了什么?提前谢谢了。我需要尽快解决。