MVC ajax Form deserizile问题

时间:2014-02-14 10:40:48

标签: jquery ajax asp.net-mvc asp.net-mvc-4

我正在尝试使用模型和文件(HttpPostedFileWrapper)发布formenctype =“multipart / form-data”的形式,并且在获得响应时返回整个页面而不仅仅是更新的部分视图。

形式:        

  <textarea rows="3" id="Message" name="Message" placeholder="Write a reply...">        </textarea>

 <button id="btnNewComment" type="submit" class="btn btn-success"><i class="icon-envelope"></i> Send</button>

<label class="control-label">Attach a file:</label>
<input type="file" name="postedFile" id="postedFile">

</form>
<div id="list">@Html.Partial("View", Model)</div>
<script>
$(function () {
        var options = {
            target: '#list',
            beforeSubmit: showRequest,  // pre-submit callback
            success: showResponse,  // post-submit callback
            error: function (err) {
                alert(err)
            },

        };
        // bind to the form's submit event 
        $('#myForm').submit(function () {
            $(this).ajaxSubmit(options);

            return false;
        });
    });

    // pre-submit callback 
    function showRequest(formData, jqForm, options) {
        var queryString = $.param(formData);

        alert('About to submit: \n\n' + queryString);

        return true;
    }

    // post-submit callback 
    function showResponse(responseText, statusText, xhr, $form) {

        alert('status: ' + statusText + '\n\nresponseText: \n' + responseText +
            '\n\nThe output div should have already been updated with the responseText.');
    }

我还应该提到我正在使用jQuery Form Plugin,我们将不胜感激。

    //
    // POST: /Create
    [HttpPost]
    public ActionResult Index(Model thread, HttpPostedFileWrapper postedFile)
    {
        if (ModelState.IsValid)
        {
            try
            {
                var model = (Class)Shared.SelectedConversation;

                model.Create(thread, postedFile);

                return View("View", model);
            }
            catch (Exception ex)
            {
                while (ex.InnerException != null)
                    ex = ex.InnerException;
                ModelState.AddModelError("Error", ex.Message);
            }
        }
        return View();
    }

0 个答案:

没有答案