-1

Form sample
I have 2 models in this form The main model is Beverage

[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; }
    }

The second model is 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 is a PartialView has its own model

<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>

As you see I have a button called addprice and another button to submit the form called create. What I want is when I click add price I validate only the price div and then I will add the price to a table under the price form because the user can add more than one price.

kartal
  • 17,436
  • 34
  • 100
  • 145
  • What do you mean "_validate the price div"_? You don't have any validation attributes on you model properties and nowhere do you render `@Html.ValidationMessageFor()` so there is nowhere to display errors anyway. Next you have not indicated how you are saving the `BeveragePrice` (are you using ajax?) nor how you add a new `BeveragePrice` so its impossible to give you an answer. Perhaps [this answer](http://stackoverflow.com/questions/29837547/set-class-validation-for-dynamic-textbox-in-a-table/29838689#29838689) might get you some hints. And stop using data models in your view! Use view models –  May 22 '15 at 03:26

1 Answers1

0

You can submit the partial view to action as BeveragePrice, so the action should look like, see the link below

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

Or If you are familiar with jQuery,You can use jQuery to validate the price, retrieve it and show it in an table below.

Community
  • 1
  • 1