如何从Asp.net Mvc

时间:2017-07-17 09:14:17

标签: jquery asp.net ajax asp.net-mvc

此模型类FOR Prouct

public partial class Product
{
    public int ProductID { get; set; }
    public int CategoryID { get; set; }
    public string ProductName { get; set; }
    public decimal Rate { get; set; }
   }

这是订单明细表的模型类

    public partial class OrderDetail
{
    public int OrderDetailsID { get; set; }
    public int OrderID { get; set; }
    public int ProductID { get; set; }
    public decimal Rate { get; set; }
    public int Quantity { get; set; }
    public decimal TotalAmount { get; set; }

    public virtual OrderMaster OrderMaster { get; set; }
 }

用于保存数据库中的条目的控制器代码

public ActionResult Create(ViewModel model) {
            OrderMaster master = new OrderMaster();

                    master.OrderNo= model.OrderNo;
                    master.OrderDate= model.OrderDate;
                    master.Description = model.Description;



            db.OrderMasters.Add(master);
            db.SaveChanges();
            OrderDetail order = new OrderDetail();

                order.ProductID= model.ProductID;
                order.Quantity=model.Quantity;
                order.Rate = model.Rate;
                order.OrderID = db.OrderMasters.Max(x => x.OrderID);

            db.OrderDetails.Add(order);
            db.SaveChanges();


            return View("Create");
        }

当我从下拉列表中选择产品时,我有一个DropDownList我需要的产品下一个TextBox填充了数据库中存在的产品的速率

<td> @Html.DropDownListFor(m => m.ProductID, new SelectList(Enumerable.Empty<SelectListItem>(), "ProductID", "ProductName"), "Select Product", htmlAttributes: new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => model.ProductID, "", new { @class = "text-danger" })

                    </td>
                    <td>
                        @Html.TextBoxFor(model => model.Rate, htmlAttributes: new { @class = "form-control" })
                    </td>
                    <td>

                        @Html.TextBoxFor(model => model.Quantity, htmlAttributes: new { @class = "form-control" })
                    </td>

                    <td>
                        @Html.TextBoxFor(model => model.TotalAmount, htmlAttributes: new { @class = "form-control" })
                    </td>

1 个答案:

答案 0 :(得分:0)

在此处更改产品ddl,然后查看填充率。这篇文章很容易做到。它很长,但请从中获取所需的东西。

查看:

@model Testy20161006.Controllers.FillRate
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>IndexStackOverflow</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {

            $("#productDDL").change(function () {
                $.ajax({
                    type: 'POST',
                    url: '@Url.Action("getRate")',
                    dataType: 'json',
                    data: { id: $("#productDDL").val() },
                    success: function (rate) {
                        $("#theRate").val(rate.rate);
                    },
                    error: function (ex) {
                        alert('Failed.' + ex);
                    }
                });
                return false;
            })
        });
    </script>
</head>
<body>
    @using (Html.BeginForm())
    {
        <table>
            <tr>
                <td>

                    @Html.DropDownListFor(m => m.ProductID, new SelectList(Model.ddlList, "Value", "Text"), "Select Product", htmlAttributes: new { id = "productDDL", @class = "form-control" })

                </td>
                <td>
                    @Html.TextBoxFor(model => model.Rate, htmlAttributes: new { id = "theRate", @class = "form-control" })
                </td>

            </tr>
        </table>
        <div><input type="submit" value="submit" /></div>
    }
</body>
</html>

控制器/型号:

public class HomeController : Controller
{
    public JsonResult getRate(int id)
    {
        //calculate rate, can use db here

        var theRate = 0M;
        //if (id == 2){
            theRate = 2.37M;
        //}
        return Json(new
        {
            rate = theRate
        }
        , JsonRequestBehavior.AllowGet);
    }

    [HttpPost]
    public ActionResult IndexStackOverflow(FillRate fillRate)
    {
        //put breakpoint here to see value of returned model
        return View(fillRate);
    }

    //use your name of action that starts the process, named in routeconfig.cs
    public ActionResult IndexStackOverflow()
    {
        List<SelectListItem> list = new List<SelectListItem>();
        list.Insert(0, new SelectListItem { Text = "Selection1", Value = "1" });
        list.Insert(0, new SelectListItem { Text = "Selection2", Value = "2" });
        list.Insert(0, new SelectListItem { Text = "Selection3", Value = "3" });
        FillRate fillRate = new FillRate();
        fillRate.ddlList = list;
        return View(fillRate);
    }