部分视图模型验证无法使用.net实体框架

时间:2017-12-27 20:52:58

标签: asp.net-mvc validation views partial

我有一个视图和一个局部视图,每个视图和部分视图在不同模型的相同表单上。数据验证正在从父视图处理模型,但没有验证正在处理局部视图。这是代码 用于具有第一个模型的视图和部分视图父视图

@using (Html.BeginForm())
{@Html.AntiForgeryToken()
<div class="form-horizontal" id="ClientView">
    <h4>@Resources.Resources.Client</h4>
    <hr />@Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div class="form-group">
            @Html.Label(@Resources.Resources.SearchForClients, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <input type="text" name="SearchText" id="SearchText" placeholder="@Resources.Resources.EmailOrPhone"><br>
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model=> model.SalutationId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-4">
            @if (@Model == null)
            {
                @Html.DropDownList("SalutationId", null, htmlAttributes: new { @class = "form-control" })
            }
            else
            {
                @Html.DropDownList("SalutationId", null, htmlAttributes: new { @class = "form-control", @readonly = "readonly" })

            }

        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @if (@Model == null)
            {

                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
            }
            else
            {
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })

            }


            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.PrimaryNumber, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @if (@Model == null )
            {

                @Html.EditorFor(model => model.PrimaryNumber, new { htmlAttributes = new { @class = "form-control" } })
            }
            else
            {
                @Html.EditorFor(model => model.PrimaryNumber, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })

            }

            @Html.ValidationMessageFor(model => model.PrimaryNumber, "", new { @class = "text-danger" })
        </div>
    </div>

        <div class="form-group">
            @Html.LabelFor(model => model.SecondaryNumber, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-5">
                @if (@Model == null)
                {

                    @Html.EditorFor(model => model.SecondaryNumber, new { htmlAttributes = new { @class = "form-control" } })
                }
                else
                {
                    @Html.EditorFor(model => model.SecondaryNumber, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })

                }
                @Html.ValidationMessageFor(model => model.SecondaryNumber, "", new { @class = "text-danger" })
            </div>
            <div >
                @Html.LabelFor(model => model.NationalCardPic, htmlAttributes: new { @class = "control-label col-md-2" })
                <div >
                    @if (@Model == null)
                    {
                    <input type="file" title="search image" id="NationalCardPath" name="NationalCardPath" onchange="show(this)"/>
                    }
                    else
                    {
                        <input type="file" title="search image" id="NationalCardPath" name="NationalCardPath" onchange="show(this)" disabled/>
                    }
                 </div>
           </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @if (@Model == null)
                {

                    @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
                }
                else
                {
                    @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })

                }


                @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
            </div>
        </div>
    <br/>
        <div class="form-group">
            @Html.Label(Resources.Resources.InsuranceType, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4">
                @Html.DropDownList("InsuranceTypeId", null, htmlAttributes: new { @class = "form-control" })
            </div>
            @Html.Label(Resources.Resources.InsuranceCompany, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4">
                @Html.DropDownList("InsuranceCompanyId", null, htmlAttributes: new { @class = "form-control" })
            </div>
        </div>
        <div id="formView">

        </div>


        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Submit" class="btn btn-default" />
            </div>
        </div>
    </div>

}

用ajax函数和另一个模型调用的局部视图

<div class="form-horizontal">
     @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.CarBrandId, htmlAttributes: new {       @class = "control-label col-md-2" })
        <div class="col-md-4">
            @Html.DropDownList("CarBrandId", null, htmlAttributes: new { @class = "form-control" })

        </div>
        @Html.LabelFor(model => model.CarTypeId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-4">
            @Html.DropDownList("CarTypeId", null, htmlAttributes: new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.CarColorId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-4">
            @Html.DropDownList("CarColorId", null, htmlAttributes: new { @class = "form-control" })
        </div>
        @Html.LabelFor(model => model.MotorNumber, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-4">
            @Html.EditorFor(model => model.MotorNumber, new { htmlAttributes = new { @class = "form-control" } })
        </div>
    </div>
    <div class="form-group">

        <div class="col-md-10">

            @Html.ValidationMessageFor(model => model.MotorNumber, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.CarModelId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-4">
            @Html.DropDownList("CarModelId", null, htmlAttributes: new { @class = "form-control" })
        </div>
        @Html.LabelFor(model => model.PaymentTypeId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-4">

            <label class="btn btn-primary active">
                @Html.RadioButton("PaymentTypeId", "1", true, new { id = "Cash" }) Cash
            </label>
            <label class="btn btn-primary">
                @Html.RadioButton("PaymentTypeId", "2", new { id = "Visa" }) Visa
            </label>

        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.YearId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-4">
            @Html.DropDownList("YearId", null, htmlAttributes: new { @class = "form-control" })
        </div>
        <div id="bankList">
            @Html.LabelFor(model => model.BankId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4">
                @Html.DropDownList("BankId", null, htmlAttributes: new { @class = "form-control" })
            </div>
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-4">
            @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
        </div>
        <div>
            @Html.LabelFor(model => model.LicensePic, htmlAttributes: new { @class = "control-label col-md-2" })
            <div>
                <input type="file" title="search image" id="licensePath" name="licensePath" onchange="show(this)" />
            </div>
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Inspection, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <div class="checkbox">
                @Html.EditorFor(model => model.Inspection)
                @Html.ValidationMessageFor(model => model.Inspection, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>
</div>

0 个答案:

没有答案