在另一个模型窗体MVC 5中验证PartialView

时间:2015-05-21 20:56:16

标签: jquery forms validation c#-4.0 asp.net-mvc-5

Form sample
我有这种形式的2个模型主要模型是饮料

[Serializable]
    public class Beverage
    {
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int BeverageId { get; set; }
        public int BeverageTypeId { set; get; }
        public string Name { set; get; }
        public string ImageUrl { get; set; }
        public string Size { get; set; }
        public string Details { get; set; }
        public bool IsAvailableForGuests { get; set; }
        public bool IsTaxable { get; set; }

        public virtual BeverageType BeverageType { set; get; }

        public virtual ICollection<BeveragePrice> BeveragePrices { set; get; }
    }

第二个模型是BeveragePrice

[Serializable]
    public class BeveragePrice
    {
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int BeveragePriceId { set; get; }
        [ForeignKey("Beverage")]
        public int BeverageId { set; get; }
        public Decimal Price { get; set; }
        public double Taxes { get; set; }
        public DateTime EffectiveDate { get; set; }
        public virtual Beverage Beverage { set; get; }
    }

BeveragePrice是PartialView有自己的模型

<div class="form-horizontal">
    <h4>Beverage Prices</h4>
    <div class="col-md-6 col-sm-10">
        <div class="form-group">
            @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "col-md-3 col-sm-4 control-label" })
            <div class="col-md-8 col-sm-7">
                @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control", @type = "number", @step = "any", @onkeypress = "return OnlyNumeric(this);" } })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Taxes, htmlAttributes: new { @class = "col-md-3 col-sm-4 control-label" })
            <div class="col-md-8 col-sm-7">
                @Html.EditorFor(model => model.Taxes, new { htmlAttributes = new { @class = "form-control", @type = "number", @step = "any", @onkeypress = "return OnlyNumeric(this);" } })
            </div>
            <div class="measures">
                <span>%</span>
            </div>
        </div>
    </div>
    <div class="col-md-6 col-sm-10">
        <div class="form-group">
            @Html.LabelFor(model => model.EffectiveDate, htmlAttributes: new { @class = "col-md-3 col-sm-4 control-label" })
            <div class="col-md-8 col-sm-7 ">
                <div class="form-control custominput">
                    @Html.EditorFor(model => model.EffectiveDate, "ClockDateTime", new { htmlAttributes = new { @class = "form-control" } })
                </div>
            </div>
        </div>
    </div>
    <div style="clear:both"></div>
    <div class="col-md-6 col-sm-10 col-right">
        <div class="form-group">
            <input  type="button" value="Add Price" class="btn btn-primary">
        </div>
    </div>
</div>

如您所见,我有一个名为addprice的按钮和另一个提交名为create的表单的按钮。我想要的是当我点击添加价格我只验证价格div然后我会将价格添加到价格表格下的表格中,因为用户可以添加多个价格。

1 个答案:

答案 0 :(得分:0)

您可以将部分视图作为BeveragePrice提交给操作,因此操作应如下所示,请参阅下面的链接

ASP.NET MVC Validation in Partial View and return to Parent view

或者 如果您熟悉jQuery,可以使用jQuery验证价格,检索它并将其显示在下表中。