使用jquery对话框在mvc中执行表单提交和验证

时间:2011-06-24 00:59:23

标签: jquery asp.net-mvc

嗨,我有一个表格和部分视图。部分视图表示文件上载内容,它包含文件名文本框和文件输入。在窗体上有一个按钮,当单击它时,它会将此部分视图加载到jquery对话框中,并带有提交和取消按钮。使用后点击提交按钮,它只需调用$(“#myform”)。submit。一切正常,但我想执行mvc验证,如果文档名称为空,我想保持对话框仍然打开并在ValidationSummary区域中显示错误。有人能告诉我如何实现这个目标

谢谢

这是我的jquery对话框代码

   submitDialog:function(url,title,event,target,frm,onLoadCallBack){
    event.preventDefault();
    $url = url;
    $title = title;
    var $dialog = $(target);
    $dialog.empty();
    $dialog
        .load($url,onLoadCallBack)            
        .dialog({
            bgiframe: true,
            title: $title,
            height: 200,
            width: 400,
            modal: true,
            autoOpen: false,
            resizable: false,                  
        }); 
    $dialog
        .dialog("option", "buttons", {
            "Submit":function(){
                var dlg = $(this);
                var $frm = $(frm);
                $frm.submit();
        },
        "Cancel": function() { 
            $(this).dialog("close");
            $(this).empty();
        }    

    });
   $dialog.dialog('open');

 }

这是我的部分观点

@model MVCWeb.Models.UploadDocBaseModel
@{
    ViewBag.Title = "Document Upload";
}

<h3>Documents</h3>
   @using (Html.BeginForm(MVC.Order.SaveOrderDoc(Model), FormMethod.Post, new { enctype =             "multipart/form-data", Id = "frmDocUpload" } ))
  {
   @Html.ValidationSummary() 
   <fieldset>        
      <ol>
        <li class="clearfix">
            <label class="fixed-width-label medium">Document Name</label>
            @Html.TextBox("docName", "", new { @class = "text-field medium" })
        </li>
        <li class="clearfix">
            <label class="fixed-width-label medium">Upload</label>
            <input type="file" id="docFile" name="fileUpload" />
        </li>          
       </ol>
   </fieldset>
  }

我的控制器

        public virtual ActionResult DocumentUpload(long idOrder)
    {
        UploadDocBaseModel docModel = new UploadDocBaseModel { IdParent = idOrder };
        //return PartialView("",docModel);
        return PartialView(Views._OrderDocUpload, docModel);
    }

    public virtual ActionResult SaveOrderDoc(UploadDocBaseModel model)
    {
        if(ModelState.IsValid)
        {

        }

        return PartialView(Views._OrderDocUpload, model);
    }

1 个答案:

答案 0 :(得分:0)

文件上传输入将被jQuery读取为文本框输入,因此您可以执行...

var filenamePath = ('#mytextboxinputforfile').val() // returns the filename in the textbox
if (filenamePath == '') {
    // show error in validation summary.  you can get this by looking at the 
    // page source to see what the validation summary id is and set the text.
}