Jquery与.net MVC结合,通过Ajax

阅读更多

在工作中做了这么一个东西。

Html端:

@using Test.fh.Project.Storefront.ViewModels
@using Test.fh.Project.Storefront.Services
@model ProductViewModel
Categories

@MvcHtmlString.Create(@Model.product_name)

@using (Html.BeginForm("index", "Product")) { @Html.ValidationSummary(excludePropertyErrors: true)
@if (Model.imagePaths.Count() > 0) {
@foreach (var item in Model.imagePaths) { @Model.product_name }
}
@if (Model.manufacturer != null || Model.manufacturer != "") { Brand: @Model.manufacturer
} Product Code: @Model.model
Reward Points: @Model.reward
Availability: @Model.stock_status
Price: @if (Model.special == null || Model.special == "") { @Currency.format(Model.price, null, null, true) } else { @Currency.format(Model.price, null, null, true) @Currency.format(Model.special, null, null, true) }
Ex Tax: @if (Model.special != null && Model.special != "") { @Currency.format(Model.special, null, null, true) } else { @Currency.format(Model.price, null, null, true) }
Price in reward points: @Model.points

@foreach (var item in ViewBag.Discounts) { @string.Format("({0} or more {1})", @item["quantity"], @Currency.format(item["price"].ToString(), null, null, true));
}
@if (ViewBag.Options != null) {

Available Options


@foreach (var item in ViewBag.Options) { if (item.Key["type"] == "select") {
@if (item.Key["required"] != null && item.Key["required"] == 1) { * } @item.Key["name"]:

} if (item.Key["type"] == "radio") {
@if (item.Key["required"] != null && item.Key["required"] == 1) { * } @item.Key["name"]:
@foreach (var option_value in item.Value) {
}

} if (item.Key["type"] == "checkbox") {
@if (item.Key["required"] != null && item.Key["required"] == 1) { * } @item.Key["name"]:
@foreach (var option_value in item.Value) {
}

} if (item.Key["type"] == "text") {
@if (item.Key["required"] != null && item.Key["required"] == 1) { * } @item.Key["name"]:

} if (item.Key["type"] == "textarea") {
@if (item.Key["required"] != null && item.Key["required"] == 1) { * } @item.Key["name"]:

} if (item.Key["type"] == "file") {
@if (item.Key["required"] != null && item.Key["required"] == 1) { * } @item.Key["name"]:
Upload File

} if (item.Key["type"] == "date") {
@if (item.Key["required"] != null && item.Key["required"] == 1) { * } @item.Key["name"]:

} if (item.Key["type"] == "datetime") {
@if (item.Key["required"] != null && item.Key["required"] == 1) { * } @MvcHtmlString.Create(@item.Key["name"]):

} if (item.Key["type"] == "time") {
@if (item.Key["required"] != null && item.Key["required"] == 1) { * } @MvcHtmlString.Create(@item.Key["name"]):

} }
}
Qty: @Html.TextBoxFor(model => model.minimum, new { maxlength = 2, name = "quantity" }) @Html.HiddenFor(model => model.product_id)   Add to Cart
   - OR -   
@if (int.Parse(Model.minimum) > 1) {
This product has a minimum quantity of @Model.minimum
}
Description @if (ViewBag.attribute_groups != null) { Specification } Reviews @if (ViewBag.relate_products != null) { Related Products (@ViewBag.relate_products.Count) }
@Html.Raw(@Model.description)
if (ViewBag.attribute_groups != null) {
@foreach (var item in ViewBag.attribute_groups) { @foreach (var item_att in item.Value) { } }
@item.Key["name"]
@item_att["name"] @item_att["text"]
}
if (ViewBag.relate_products != null) { } if (ViewBag.tags != null) {
Tags: @foreach (var item in ViewBag.tags) { @item["tag"] }
} }
@*@Url.Action("Update", "Product");*@ @foreach (var item in ViewBag.Options) { if (item.Key["type"] == "file") { } }

 后台端

[HttpPost]
        public JsonResult Update()
        {
            Dictionary jsonMesg_dict = new Dictionary();

            ProductService pservice = new ProductService();
            SessionEntityService sessionService = new SessionEntityService();
            //Request.Params["××××"];是重点
            string product_id = Request.Params["product_id"];
            Dictionary options = this.arrayFilterOption(Request.Params);
            string key = product_id + ":" + new OpenSharp.Storefront.Models.Tool.PHPSerializer().Serialize(options);
            Entity product = null;

            if (product_id != null)
            {
                int customer_group_id = 8;
                product = pservice.getProduct(int.Parse(product_id), customer_group_id, languagedId);
            }

            if (product != null)
            {
                int product_total = 0;
                int quantity = 1;
                if (Request.Params["minimum"] != null)
                {
                    quantity = int.Parse(Request.Params["minimum"]);

                    SessionEntity session = sessionService.GetSessionEntity();

                    ShopCart cartItem = session.ShopCart;

                    foreach (KeyValuePair sp in cartItem.Items)
                    {
                        if (sp.Key.Equals(key))
                        {
                            product_total = sp.Value.Quantity;
                        }
                    }

                    string str_error_json = string.Empty;
                    if (int.Parse(product["minimum"].ToString()) > (product_total + quantity))
                    {
                        str_error_json = string.Format("Minimum order amount for {0} is {1}!", product["name"], product["minimum"]);
                        jsonMesg_dict.Add("warning", str_error_json);
                    }

                    Dictionary poptions = pservice.getProductOptions(int.Parse(product_id), languagedId);

                    Dictionary dic_error_json = new Dictionary();
                    jsonMesg_dict.Add("option_error", dic_error_json);
                    foreach (var item in poptions)
                    {
                        int optionID = int.Parse(item.Key["product_option_id"].ToString());
                        if (int.Parse(item.Key["required"].ToString()) == 1)
                        {
                            string str = "option[" + optionID + "]";
                            if (!options.ContainsKey(str) || (options[str] == null || options[str].Equals("")))
                            {
                                string error_json = string.Format("{0} required!", item.Key["name"]);
                                dic_error_json.Add(optionID.ToString(), error_json);
                            }
                        }
                    }

                    if (jsonMesg_dict.Count == 0)
                    {
                        ShopProduct tempShoppingP = new ShopProduct();
                        tempShoppingP.ProductId = int.Parse(product_id);
                        tempShoppingP.Options = new OpenSharp.Storefront.Models.Tool.PHPSerializer().Serialize(options);
                        tempShoppingP.Quantity = quantity;

                        //Please pay attention on the following line.
                        //                        if (product["special"] == null || product["special"] == "")
                        if (string.IsNullOrEmpty(product["special"].ToString()))
                        {
                            tempShoppingP.UnitPrice = double.Parse(product["price"].ToString());
                        }
                        else
                        {
                            tempShoppingP.UnitPrice = double.Parse(product["special"].ToString());
                        }
                        tempShoppingP.ItemCalculate();

                        cartItem.Calculate(tempShoppingP);

                        jsonMesg_dict.Add("success", product["name"].ToString());
                    }
                    else
                    {
                    }
                }
            }
            return Json(jsonMesg_dict, JsonRequestBehavior.AllowGet);
        }

你可能感兴趣的:(JavaScript,Ajax,.net,MVC)