Do not forget to dig well people, if you have help, please say thank you. If you want to reprint, please indicate the source. Thank you!
When an asynchronous commit occurs, the picture cannot be uploaded.
At first I froze in the SYSTEM.WEB.MVC. When querying the source code, there is no problem. Where did the problem arise?
Answer: JS
Jquery.unobtrusive-ajax.js
After viewing, modify the following demo:
functionasyncrequest (element, options) {varConfirm, loading, method, duration, enctype, Is_async =true; Confirm= Element.getattribute ("Data-ajax-confirm"); if(Confirm &&!)window.confirm (confirm)) { return; } Loading= $ (Element.getattribute ("data-ajax-loading")); Duration= Element.getattribute ("data-ajax-loading-duration") | | 0; $.extend (Options, {Type:element.getAttribute ("Data-ajax-method") | |Undefined, Url:element.getAttribute ("Data-ajax-url") | |Undefined, beforesend:function(XHR) {varresult; Asynconbeforesend (xhr, method); Result= GetFunction (Element.getattribute ("Data-ajax-begin"), ["XHR"]). Apply ( This, arguments); if(Result!==false) {loading.show (duration); } returnresult; }, Complete:function() {loading.hide (duration); GetFunction (Element.getattribute ("Data-ajax-complete"), ["Xhr", "status"]). Apply ( This, arguments); }, Success:function(data, status, XHR) {asynconsuccess (element, data, Xhr.getresponseheader ("Content-type") | | "Text/html"); GetFunction (Element.getattribute ("Data-ajax-success"), ["Data", "status", "XHR"]). Apply ( This, arguments); }, Error:getfunction (Element.getattribute ("Data-ajax-failure"), ["Xhr", "status", "Error"]) }); Enctype= Element.getattribute ("Enctype"); if(enctype = = "Multipart/form-data") { varFormData =NewFormData ($ (element) [0]); $.extend (Options, {async:false, Cache:false, ContentType:false, ProcessData:false, data:formdata}); Is_async=false; } if(Is_async) Options.data.push ({name:"X-requested-with", Value: "XMLHttpRequest" }); Method=options.type.toUpperCase (); if(!Ismethodproxysafe (method)) {Options.type= "POST"; if(Is_async) Options.data.push ({name:"X-http-method-override", Value:method}); } $.ajax (options); }
@{viewbag.title = "Index";}<Scriptsrc= "~/scripts/jquery-1.8.2.js"></Script><Scriptsrc= "~/scripts/modernizr-2.6.2.js"></Script><Scriptsrc= "~/scripts/jquery.unobtrusive-ajax.js"></Script><Scriptsrc= "~/scripts/jquery.validate.js"></Script><Scriptsrc= "~/scripts/jquery.validate.unobtrusive.js"></Script>@model MvcApplication.Models.TestModel @using (Ajax.beginform ("Testajax", "Home", NULL, New Ajaxoptions () { HttpMethod = "POST", onsuccess= "Success"}, new {id = "F1", enctype = "Multipart/form-data"}) {@Html. textboxfor (S =& Gt S.name) @Html. textboxfor (s = = s.age) @Html. textboxfor (s = = s.filebase, new {type = "file"}) @Html. Text Boxfor (S = = S.isdel, new {Value = "1"})<Buttontype= "Submit">Ok</Button> }<Scripttype= "Text/javascript"> functionSuccess (data) {alert (data). Name); }</Script>
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;namespacemvcapplication.controllers{ Public classHomecontroller:controller {// //GET:/home/ PublicActionResult Index () {returnView (); } PublicActionResult Testajax (Models.testmodel model) {Model.filebase=NULL; returnJson (model, jsonrequestbehavior.allowget); } }}
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;namespacemvcapplication.models{ Public classTestmodel { PublicHttpPostedFileBase FileBase {Get;Set; } Public stringName {Get;Set; } Public intAge {Get;Set; } Public BOOLIsdel {Get;Set; } }}
MVC Ajax.beginform Submit Upload Image