如何在同一视图中使用相同模型从多个部分视图中检索数据?

时间:2013-12-01 11:37:25

标签: asp.net-mvc asp.net-mvc-4 partial-views

我是ASP.NET MVC的新手,但我正在创建一个像Edfa3ly.com这样的网站,用户应该订购多个项目。我使用@Ajax.ActionLink让他添加另一个项目(通过再次调用相同的局部视图)问题是:当点击保存订单时它只保存1个项目(它覆盖了第一个)我不知道如何保存第一个项目然后再拿另一个..我试图放@Html.ActionLink但它不验证表单,也传递空模型。

这就是观点:

@model CSP1225.Models.OrderItemsModel

@{
    ViewBag.Title = "MakeOrder";
    Layout = "~/Views/Shared/_Layout.cshtml";
}




@using (Html.BeginForm("MakeOrder","Order",Model)) {
    @Html.AntiForgeryToken()
    <div class="container">
        <div>
    <div class="inner">

        @Html.LabelFor(m=>m.NewOrderDetails.fees)
        @Html.DisplayFor(m=>m.NewOrderDetails.fees)

        @Html.LabelFor(m=>m.NewOrderDetails.totalPrice)
        @Html.DisplayFor(m=>m.NewOrderDetails.totalPrice)

        @Html.LabelFor(m=>m.NewOrderDetails.totalWeight)
        @Html.DisplayFor(m=>m.NewOrderDetails.totalWeight)



  @Html.Partial("NewItem",Model.itemAdded)

       <div id="anotherItem">

       </div>


       @Ajax.ActionLink("another item", 
                 "NewItem",
                 new AjaxOptions { 
                 UpdateTargetId = "anotherItem"})
    </div>
            <input type="Submit" value="Save Order" />
        </div>
        </div>

}

这是部分观点:

@model CSP1225.Models.ItemDetails


@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <legend>Item Details</legend>
        <div class="inner">
          @Html.ValidationSummary()

        @Html.LabelFor(m=>m.itemName)
        @Html.TextBoxFor(m=>m.itemName)


        @Html.LabelFor(m=>m.itemUrl)
        @Html.TextBoxFor(m=>m.itemUrl)

        @Html.LabelFor(m=>m.quantity)
        @Html.TextBoxFor(m=>m.quantity)

        @Html.LabelFor(m=>m.weight)
        @Html.TextBoxFor(m=>m.weight)

        @Html.LabelFor(m=>m.unitprice)
        @Html.TextBoxFor(m=>m.unitprice)

        @Html.LabelFor(m=>m.Notes)
        @Html.TextBoxFor(m=>m.Notes)
<br />

        @Html.ActionLink("save item","NewItem","Order",Model.itemAdded,null)


</div>

        </div>
}

那是我的模特:

        public class OrderItemsModel
        {
            public NewOrder NewOrderDetails { get; set; }
            public ItemDetails itemAdded { get; set; }

        }

     public class ItemDetails
        {
            [Required(ErrorMessage = "name Required")]
            public string itemName { get; set; }
            [Required(ErrorMessage = "URL Required")]
            [DataType(DataType.Url)]
            public string itemUrl { get; set; }
            public int quantity { get; set; }
            [Required(ErrorMessage = "price Required")]
            [DataType(DataType.Currency)]
            public Decimal unitprice { get; set; }
            public string color { get; set; }
            public string Size { get; set; }
            [Required(ErrorMessage = "weight Required")]
            public double weight { get; set; }
            public string Notes { get; set; }
            public IList<Item> itemList { get; set; }
        }

     public class NewOrder
        {

            [Required]
            [Display(Name = "Total weight")]
            public double totalWeight { get; set; }
            [Required]
            [Display(Name = "Total Price")]
            public decimal totalPrice { get; set; }
            [Required]
            [Display(Name = "Fees")]
            public double fees { get; set; }

            public DateTime OrderDate = DateTime.Now;
}

那是控制器:

 Order ord = new Order();
 ItemDetails it;
        List<Item> lstItem = new List<Item>();
       decimal tp = 0;
        double tw = 0;
        double f = 5.5;


    public PartialViewResult NewItem(OrderItemsModel item)
        {


return PartialView();

        }

    public PartialViewResult NewItemAdd(ItemDetails item)
    {
     if (item !=null)
     {
        it = new ItemDetails();
        it = item;
        CDB1225Entities _context = new CDB1225Entities();
        Item i = new Item();

        if (it != null)
        {
            i.ItemName = item.itemName;
            i.ItemURL = item.itemUrl;
            i.Notes = item.Notes;
            i.Price = item.unitprice;
            i.Quantity = item.quantity;
            i.Weight = item.weight;
            _context.Items.Add(i);

            lstItem.Add(i);
            tw = tw + i.Weight;
            tp = tp + i.Price;
            if (i.Weight >= 0.5)
            {
                f = (i.Weight / 0.5) * f;

            }
            else
            {
                f = 5.5;
            }

            item = null;
        }
            return PartialView();
        }
        return PartialView();
    }
}

1 个答案:

答案 0 :(得分:0)

您是否看到了实际编写的HTML代码? 您正在撰写<form> 2个Makeorder内的详细信息,我们的想法是,您只需附加表单控件,而不是新的<form>标记,就像您的@using (Html.BeginForm())调用一样:

从局部视图中删除Html.BeginForm(它将嵌入到主窗体中),您需要按照与对象相同的名称约定,换句话说:

您将从模型中itemName致电Model.itemAdded.itemName,因此您需要在部分视图中遵循相同的约定:

<div class="form-horizontal">

    <legend>Item Details</legend>


    <label for="itemAdded.itemName">
       @Html.TextBox("itemAdded.itemName", Model.itemName)
    </label>

    <label for="itemAdded.itemUrl">
       @Html.TextBox("itemAdded.itemUrl", Model.itemUrl)
    </label>

    <label for="itemAdded.quantity">
       @Html.TextBox("itemAdded.quantity", Model.quantity)
    </label>

    <label for="itemAdded.weight">
       @Html.TextBox("itemAdded.weight", Model.weight)
    </label>

    <label for="itemAdded.unitprice">
       @Html.TextBox("itemAdded.unitprice", Model.unitprice)
    </label>

    <label for="itemAdded.Notes">
       @Html.TextBox("itemAdded.Notes", Model.Notes)
    </label>


</div>

不理解Save按钮,因为主窗体可以有Save按钮本身,只需将其添加到那里。

Controller的{​​{1}}行动中,你需要拥有的只是:

Orders/MakeOrder