Validation 验证和提交按钮don';当使用复合模型时,t函数在PartialView中一起运行

Validation 验证和提交按钮don';当使用复合模型时,t函数在PartialView中一起运行,validation,asp.net-mvc-4,visual-studio-2012,razor,partial-views,Validation,Asp.net Mvc 4,Visual Studio 2012,Razor,Partial Views,表单中的验证在模型中定义 如果我的视图中没有包含这两行: $("#frmNewTemplate").removeData("validator").removeData("unobtrusiveValidation"); $.validator.unobtrusive.parse("#frmNewTemplate"); “提交”按钮工作正常,但验证不起作用。如果我包括这些,验证开始工作,提交按钮不再工作 我试图通过我的模型进行验证 我的模型: public class TemplateDTO

表单中的验证在模型中定义

如果我的视图中没有包含这两行:

$("#frmNewTemplate").removeData("validator").removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse("#frmNewTemplate");
“提交”按钮工作正常,但验证不起作用。如果我包括这些,验证开始工作,提交按钮不再工作

我试图通过我的模型进行验证

我的模型:

public class TemplateDTO : DTOBase
    {
        public virtual int ID { get; set; }
        [Required(ErrorMessage="فيلد ضروري")]
        [RegularExpression("^[a-zA-z0-9\\-\\. ]+$", ErrorMessage = "کاراکترهاي وارد شده در محدوده کاراکترهاي مجاز نمي باشند")]
        public virtual string Name { get; set; }
        [RegularExpression("^[a-zA-zآ-ي0-9\\-\\. ]+$", ErrorMessage = "کاراکترهاي وارد شده در محدوده کاراکترهاي مجاز نمي باشند")]
        public virtual string Name_Fr { get; set; }
        public virtual IList<TemplateDetailsDTO> LstDetails { get; set; }
    }
public类TemplateDTO:DTOBase
{
公共虚拟整数ID{get;set;}
[必需(ErrorMessage=“فلدضووي”)]
[正规表达(^[a-a-zA-z0-10-10-10-10-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-10 10-10-10-10 10 10 10-10 10 10 10 10 10-10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10,,,,,,,,,,,,,,,,,、错误消息,错误消息,错误消息,错误消息,错误消息,错误消息,错误消息,误差消息=“若若若若若若若若若若若若若若若若若若若从从从从从151515151575 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5)]
公共虚拟字符串名称{get;set;}
[正规表达(“a-a-扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎扎10 10 10-9 10 10 10 10 10-10 10 10 10 10 10 10 10 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5,,,,,”错误消息,错误消息,错误消息中,误差消息,误差消息,若若若若若若若若若若若若15 15 15 15 15 15 15 15 15 5 5 5 5 5 5 5 5 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15اشند“)]
公共虚拟字符串名称\u Fr{get;set;}
公共虚拟IList LstDetails{get;set;}
}
我的复合模型:

public class TemplateViewModel
    {
        public TemplateDTO Template { get; set; }
        public List<TemplateFieldsDTO> TemplateFeilds { get; set; }
        public int[] oSelectedFieldsDTO { get; set; }
        public string Text { get; set; }
        public bool NextLine { get; set; }
    }
公共类TemplateViewModel
{
公共模板到模板{get;set;}
公共列表模板feilds{get;set;}
public int[]oSelectedFieldsDTO{get;set;}
公共字符串文本{get;set;}
公共布尔下一行{get;set;}
}
我的控制器(调用视图的ActionResult和作为答案的JsonResult):

public ActionResult\u NewTemplate()
{
如果(myGlobalVariables.AllTemplateFieldsDTO==null)
{
TemplateFieldsClientService oTemplateFieldsClientService=新TemplateFieldsClientService();
myGlobalVariables.AllTemplateFieldsDTO=oTemplateFieldsClientService.GetAll();
}
TemplateDTO-oTemplateDTO=新的TemplateDTO();
oTemplateDTO.LstDetails=新列表();
TemplateViewModel x=新的TemplateViewModel();
x、 TemplateFeilds=myGlobalVariables.AllTemplateFieldsDTO;
x、 模板=oTemplateDTO;
返回局部视图(x);
}
[HttpPost]
公共JsonResult_NewTemplate(TemplateViewModel或TemplateViewModel)
{
if(ModelState.IsValid)
{
if(oTemplateViewModel.Template!=null)
{
if(oTemplateViewModel.Template.Name==null)
{
抛出新的System.ArgumentNullException(“参数不能为null”,“oTemplateDTO”);
}
字符串strAttr=“”;
if(oTemplateViewModel.NextLine)
strAttr=“\n”;
List lstemdetails=新列表();
int位置=0;
if(oTemplateViewModel.oSelectedFieldsDTO!=null)
{
foreach(oTemplateViewModel.oSelectedFieldsDTO中的变量i)
{
TemplateDetailsTo TempDetls=新的TemplateDetailsTo();
TempDetls.PositionNumber=++位置;
TempDetls.Attributes=strAttr;
TemplateFieldsTo-OtemplateFieldsTo=myGlobalVariables.AllTemplateFieldsTo.Find(x=>x.ID==i);
TempDetls.ObjFields=OTemplateFieldsDTO;
TempDetls.ObjTemplate=oTemplateViewModel.Template;
lstemdetails.Add(TempDetls);
}
}
oTemplateViewModel.Template.LstDetails=lstItemDetails;
oTemplateClientService.AddNewTemplate(oTemplateViewModel.Template);
myGlobalVariables.AllTemplateDTO=oTemplateClientService.GetAllTemplate();
log.Info(“添加到数据库的模板”);
返回Json(新的{Result=“OK”,Message=“عمليت㶭㶪㶪مو㶩㶩㶬㶩},JsonRequestBehavior.DenyGet);
}
其他的
{
返回Json(新的{Result=“Error”,Message=“لطافففه㶈㶈㶈㶈㶈㶈”JsonRequestBehavior.DenyGet);
}
}
其他的
{
返回Json(新的{Result=“Error”,Message=“ل;
}
}
我的观点代码:

@model IAC.SMS.MvcApp.Controllers.TemplateViewModel


<script type="text/javascript">


    $(function () {

            $("#frmNewTemplate").removeData("validator").removeData("unobtrusiveValidation");
            $.validator.unobtrusive.parse("#frmNewTemplate");

        $('#btnAdd').click(function () {
            $('#ListBoxSource').find('option:selected').appendTo('#oSelectedFieldsDTO');
        })

        $('#btnDelete').click(function () {
            $('#oSelectedFieldsDTO').find('option:selected').appendTo('#ListBoxSource');
        });

        $('#btnUp').click(function () {
            var selectedOption = $('#oSelectedFieldsDTO').find('option:selected');
            var prevOption = $('#oSelectedFieldsDTO').find('option:selected').prev("option");
            if ($(prevOption).text() != "") {
                $(selectedOption).remove();
                $(prevOption).before($(selectedOption));
            }
        });

        $('#btnDown').click(function () {
            var selectedOption = $('#oSelectedFieldsDTO').find('option:selected');
            var nextOption = $('#oSelectedFieldsDTO').find('option:selected').next("option");
            if ($(nextOption).text() != "") {
                $(selectedOption).remove();
                $(nextOption).after($(selectedOption));
            }
        });

    });

    function display() {
        var dpt = document.getElementById("oSelectedFieldsDTO");
        if (dpt.options[dpt.selectedIndex].text == "Text") {
            document.getElementById("Text").disabled = false;
        }

    };
    function SelectAllItems() {
        $("#oSelectedFieldsDTO").each(function () {
            $("#oSelectedFieldsDTO option").attr("selected", "selected");
        });
    };

    //functions for ajax begin form
    function Refresh() {
        window.location.href = "@Url.Action("ListTemplate", "Template")" + "/";
    }

    function Success(data) {
        if (data.Result == "OK") {
            jSuccess(
                                data.Message,
                                {
                                    autoHide: true, // added in v2.0
                                    clickOverlay: false, // added in v2.0
                                    MinWidth: 200,
                                    TimeShown: 1500,
                                    ShowTimeEffect: 1000,
                                    HideTimeEffect: 500,
                                    LongTrip: 20,
                                    HorizontalPosition: 'center',
                                    VerticalPosition: 'center',
                                    ShowOverlay: true,
                                    ColorOverlay: '#000',
                                    OpacityOverlay: 0.3,

                                });
            Refresh();
        }
        else {
            jSuccess(
                                data.Message,
                                {
                                    autoHide: true, // added in v2.0
                                    clickOverlay: false, // added in v2.0
                                    MinWidth: 200,
                                    TimeShown: 1500,
                                    ShowTimeEffect: 1000,
                                    HideTimeEffect: 500,
                                    LongTrip: 20,
                                    HorizontalPosition: 'center',
                                    VerticalPosition: 'center',
                                    ShowOverlay: true,
                                    ColorOverlay: '#000',
                                    OpacityOverlay: 0.3,

                                });
        }
    }
    function Fail() {
        jSuccess(
                            'ارتباط برقرار نشد',
                            {
                                autoHide: true, // added in v2.0
                                clickOverlay: false, // added in v2.0
                                MinWidth: 200,
                                TimeShown: 1500,
                                ShowTimeEffect: 1000,
                                HideTimeEffect: 500,
                                LongTrip: 20,
                                HorizontalPosition: 'center',
                                VerticalPosition: 'center',
                                ShowOverlay: true,
                                ColorOverlay: '#000',
                                OpacityOverlay: 0.3,

                            });
    }


    </script>

@using (Ajax.BeginForm("_NewTemplate", "Template", new AjaxOptions
        {
            InsertionMode = InsertionMode.Replace, 
            HttpMethod = "POST",
            OnSuccess = "Success",
            OnFailure = "Fail",
            LoadingElementId = "Loading"
        }, new { @id = "frmNewTemplate" }))
{
    <table id="tblDetails" class="dataInput" style="width:500px;">
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td>
                    <label>
                        ليست مبدا
                    </label>
                </td>
            </tr>
            <tr>
                <td>
                    <label>
                        نام قالب
                    </label>
                </td>
                <td>
                    @Html.TextBoxFor(model => model.Template.Name)
                    <label style="color:red; font-weight:bold;">*</label>
                    @Html.ValidationMessageFor(model => model.Template.Name)
                </td>
                <td></td>
                <td rowspan="3">
                    @Html.ListBox("ListBoxSource", new SelectList(Model.TemplateFeilds, "ID", "Name", 1), new { size = 10 , style = "width:200px"})
                </td>
            </tr>
        <tr>
            <td>
                    <label>
                        توضيحات
                    </label>
                </td>
                <td>
                    @Html.TextBoxFor(model => model.Template.Name_Fr)
                </td>
        </tr>
            <tr>
                <td>
                    <label>
                        پيام تکميلي
                    </label>
                </td>
                <td>
                    @Html.TextBoxFor(model => model.Text , new { disabled="disabled"})
                </td>
                <td>
                </td>

            </tr>
        <tr>
            <td>
                @Html.CH_CheckBoxFor(model=>model.NextLine, "خط بعد", false, false)
            </td>

            <td></td>
            <td></td>
            <td>
                    <button class="fixed button" id="btnAdd" type="button">اضافه<span class="left"></span> </button>
                    <button class="fixed button" id="btnDelete" type="button">حذف<span class="right"></span> </button>
                </td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td>
                    <label>
                        ليست مقصد
                    </label>
                </td>
        </tr>
            <tr>
                <td>

                </td>
                <td></td>
                <td></td>
                <td>
                    @Html.ListBoxFor(model => model.oSelectedFieldsDTO, new SelectList(Model.Template.LstDetails, "ID", "Name", 1), new { size = 10, style = "width:200px", onchange = "display()" })
                </td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td>
                </td>
                <td>
                    <button class="fixed button" id="btnUp" type="button">بالا<span class="up"></span> </button>
                    <button class="fixed button" id="btnDown" type="button">پايين<span class="down"></span> </button>
                </td>
            </tr>
            <tr>
                <td>
                    @Html.HiddenFor(model=>model.Template.ID)
                </td>
            </tr>
        </table>
    <table>
            <tr> 
                <td></td>
                <td>
                    <div>
                        <button class="fixed button" type="submit" onmouseover="SelectAllItems()">ذخيره</button>
                    </div>
                </td>                 
            </tr>
        </table>
}
@model IAC.SMS.MvcApp.Controllers.TemplateViewModel
$(函数(){
$(“#frmNewTemplate”).removeData(“验证方”).removeData(“不引人注目的验证”);
$.validator.unobtrusive.parse(“#frmNewTemplate”);
$('#btnAdd')。单击(函数(){
$(“#ListBoxSource”).find('option:selected').appendTo('#oSelectedFieldsDTO');
})
$('#btnDelete')。单击(函数(){
$('oSelectedFieldsDTO')。查找('option:selected')。附加到('ListBoxSource');
});
$('#btnUp')。单击(函数(){
var selectedOption=$('oSelectedFieldsDTO')。查找('option:selected');
var prevOption=$('oSelectedFieldsDTO').find('option:selected').prev(“option”);
如果($(prevOption).text()!=“”){
$(选择选项).remove();
$(prevOption)。在($(selectedOption))之前;
}
});
$('#btnDown')。单击(函数(){
var selectedOption=$('oSelectedFieldsDTO')。查找('option:selected');
var nextOption=$('oSelectedFieldsDTO')。查找('option:selected')。下一步(“option”);
如果($(下一选项).text()!=“”){
$(选择选项)。删除
@model IAC.SMS.MvcApp.Controllers.TemplateViewModel


<script type="text/javascript">


    $(function () {

            $("#frmNewTemplate").removeData("validator").removeData("unobtrusiveValidation");
            $.validator.unobtrusive.parse("#frmNewTemplate");

        $('#btnAdd').click(function () {
            $('#ListBoxSource').find('option:selected').appendTo('#oSelectedFieldsDTO');
        })

        $('#btnDelete').click(function () {
            $('#oSelectedFieldsDTO').find('option:selected').appendTo('#ListBoxSource');
        });

        $('#btnUp').click(function () {
            var selectedOption = $('#oSelectedFieldsDTO').find('option:selected');
            var prevOption = $('#oSelectedFieldsDTO').find('option:selected').prev("option");
            if ($(prevOption).text() != "") {
                $(selectedOption).remove();
                $(prevOption).before($(selectedOption));
            }
        });

        $('#btnDown').click(function () {
            var selectedOption = $('#oSelectedFieldsDTO').find('option:selected');
            var nextOption = $('#oSelectedFieldsDTO').find('option:selected').next("option");
            if ($(nextOption).text() != "") {
                $(selectedOption).remove();
                $(nextOption).after($(selectedOption));
            }
        });

    });

    function display() {
        var dpt = document.getElementById("oSelectedFieldsDTO");
        if (dpt.options[dpt.selectedIndex].text == "Text") {
            document.getElementById("Text").disabled = false;
        }

    };
    function SelectAllItems() {
        $("#oSelectedFieldsDTO").each(function () {
            $("#oSelectedFieldsDTO option").attr("selected", "selected");
        });
    };

    //functions for ajax begin form
    function Refresh() {
        window.location.href = "@Url.Action("ListTemplate", "Template")" + "/";
    }

    function Success(data) {
        if (data.Result == "OK") {
            jSuccess(
                                data.Message,
                                {
                                    autoHide: true, // added in v2.0
                                    clickOverlay: false, // added in v2.0
                                    MinWidth: 200,
                                    TimeShown: 1500,
                                    ShowTimeEffect: 1000,
                                    HideTimeEffect: 500,
                                    LongTrip: 20,
                                    HorizontalPosition: 'center',
                                    VerticalPosition: 'center',
                                    ShowOverlay: true,
                                    ColorOverlay: '#000',
                                    OpacityOverlay: 0.3,

                                });
            Refresh();
        }
        else {
            jSuccess(
                                data.Message,
                                {
                                    autoHide: true, // added in v2.0
                                    clickOverlay: false, // added in v2.0
                                    MinWidth: 200,
                                    TimeShown: 1500,
                                    ShowTimeEffect: 1000,
                                    HideTimeEffect: 500,
                                    LongTrip: 20,
                                    HorizontalPosition: 'center',
                                    VerticalPosition: 'center',
                                    ShowOverlay: true,
                                    ColorOverlay: '#000',
                                    OpacityOverlay: 0.3,

                                });
        }
    }
    function Fail() {
        jSuccess(
                            'ارتباط برقرار نشد',
                            {
                                autoHide: true, // added in v2.0
                                clickOverlay: false, // added in v2.0
                                MinWidth: 200,
                                TimeShown: 1500,
                                ShowTimeEffect: 1000,
                                HideTimeEffect: 500,
                                LongTrip: 20,
                                HorizontalPosition: 'center',
                                VerticalPosition: 'center',
                                ShowOverlay: true,
                                ColorOverlay: '#000',
                                OpacityOverlay: 0.3,

                            });
    }


    </script>

@using (Ajax.BeginForm("_NewTemplate", "Template", new AjaxOptions
        {
            InsertionMode = InsertionMode.Replace, 
            HttpMethod = "POST",
            OnSuccess = "Success",
            OnFailure = "Fail",
            LoadingElementId = "Loading"
        }, new { @id = "frmNewTemplate" }))
{
    <table id="tblDetails" class="dataInput" style="width:500px;">
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td>
                    <label>
                        ليست مبدا
                    </label>
                </td>
            </tr>
            <tr>
                <td>
                    <label>
                        نام قالب
                    </label>
                </td>
                <td>
                    @Html.TextBoxFor(model => model.Template.Name)
                    <label style="color:red; font-weight:bold;">*</label>
                    @Html.ValidationMessageFor(model => model.Template.Name)
                </td>
                <td></td>
                <td rowspan="3">
                    @Html.ListBox("ListBoxSource", new SelectList(Model.TemplateFeilds, "ID", "Name", 1), new { size = 10 , style = "width:200px"})
                </td>
            </tr>
        <tr>
            <td>
                    <label>
                        توضيحات
                    </label>
                </td>
                <td>
                    @Html.TextBoxFor(model => model.Template.Name_Fr)
                </td>
        </tr>
            <tr>
                <td>
                    <label>
                        پيام تکميلي
                    </label>
                </td>
                <td>
                    @Html.TextBoxFor(model => model.Text , new { disabled="disabled"})
                </td>
                <td>
                </td>

            </tr>
        <tr>
            <td>
                @Html.CH_CheckBoxFor(model=>model.NextLine, "خط بعد", false, false)
            </td>

            <td></td>
            <td></td>
            <td>
                    <button class="fixed button" id="btnAdd" type="button">اضافه<span class="left"></span> </button>
                    <button class="fixed button" id="btnDelete" type="button">حذف<span class="right"></span> </button>
                </td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td>
                    <label>
                        ليست مقصد
                    </label>
                </td>
        </tr>
            <tr>
                <td>

                </td>
                <td></td>
                <td></td>
                <td>
                    @Html.ListBoxFor(model => model.oSelectedFieldsDTO, new SelectList(Model.Template.LstDetails, "ID", "Name", 1), new { size = 10, style = "width:200px", onchange = "display()" })
                </td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td>
                </td>
                <td>
                    <button class="fixed button" id="btnUp" type="button">بالا<span class="up"></span> </button>
                    <button class="fixed button" id="btnDown" type="button">پايين<span class="down"></span> </button>
                </td>
            </tr>
            <tr>
                <td>
                    @Html.HiddenFor(model=>model.Template.ID)
                </td>
            </tr>
        </table>
    <table>
            <tr> 
                <td></td>
                <td>
                    <div>
                        <button class="fixed button" type="submit" onmouseover="SelectAllItems()">ذخيره</button>
                    </div>
                </td>                 
            </tr>
        </table>
}