开发者

using jquery dialog perform form submit and validation in mvc

Hi I had a form and a partial view. The partial view represents the file upload content, it contains file name textbox and file input. On the form there is a button, when clicked, it loads this partial view into jquery dialog with submit and cancel button. After use clicks submit button, it simply called $("#myform").submit. Everything works fine, but I want to perform mvc validation, if the document name is empty, I'd like to keep the dialog still open and display the errors in the ValidationSummary area. Can someone give me some idea how to achieve this

Thanks

Here is my jquery dialog box code

   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');

 }

This is my partial view

@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() 
   &l开发者_Python百科t;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>
  }

My Controller

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


The file upload input will be read by jQuery as a Textbox input so you can do ...

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.
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜