c# - MVC-4 Change ViewBag.Message on server side? -
i beginner in mvc coding.
when application starts, viewbag.message is: choose file upload.
after successful upload, changes to: file uploaded successfully!
is there way can make return , show "choose file upload" message again after around 5 seconds, without using javascript ? thought if mvc had built in time function use maybe ?
https://github.com/xoxotw/mvc_fileuploader
my view:
@{ viewbag.title = "fileupload"; } <h2>fileupload</h2> <h3>upload file:</h3> @using (html.beginform("fileupload", "home", formmethod.post, new {enctype = "multipart/form-data"})) { @html.validationsummary(); <input type="file" name="filetoupload" /><br /> <input type="submit" name="submit" value="upload" /> @viewbag.message } my controller:
using system; using system.collections.generic; using system.io; using system.linq; using system.web; using system.web.mvc; namespace mvc_fileuploader.controllers { public class homecontroller : controller { public actionresult index() { viewbag.message = "choose file upload!"; return view("fileupload"); } [httppost] public actionresult fileupload(httppostedfilebase filetoupload) { if (modelstate.isvalid) { if (filetoupload != null && filetoupload.contentlength > (1024 * 1024 * 1)) // 1mb limit { modelstate.addmodelerror("filetoupload", "your file large. maximum size allowed 1mb !"); } else { string filename = path.getfilename(filetoupload.filename); string directory = server.mappath("~/fileuploads/"); if (!directory.exists(directory)) { directory.createdirectory(directory); } string path = path.combine(directory, filename); filetoupload.saveas(path); modelstate.clear(); viewbag.message = "file uploaded successfully!"; } } return view("fileupload"); } public actionresult about() { viewbag.message = "your app description page."; return view(); } public actionresult contact() { viewbag.message = "your contact page."; return view(); } } }
the short answer no. guessing because "new" want focus on mvc part mvc , javascript interlinked, think client (javascript) , server (mvc) , should master both make websites.
normally server doesn't fire events browser , instead browser make requests. there ways server raise events on client using things signalr overkill in scenario.
finally... trying achieve client-side action, i.e. inform user something. if did in mvc waste network bandwidth , add delays (think of server calls expensive) when client action , should done in javascript.
don't shy away javascript. embrace it. jquery takes lot of heavy lifting away you.
Comments
Post a Comment