c# - MVC Model and Controller for Uploaded files -
my fellow community, want ensure develop correctly. working on asp.net mvc 5 application, , while developing application studying architecture of mvc ensure grasp architecture of mvc.
i have view (see below) allow multiple files uploaded on click of submit button.
<h2>upload multiple documents</h2> @using (html.beginform("upload", "home", formmethod.post, new { enctype = "multipart/form-data" })) { <label>logo file: </label> @(html.textboxfor(m => m.file, new { type = "file", id = "logofile" })) <label>privacy pdf: </label> @(html.textboxfor(model => model.file, new { type = "file", id = "privacyfile" })) <label>payment terms pdf: </label> @(html.textboxfor(model => model.file, new { type = "file", id = "paymentterms" })) <label>faq file: </label> @(html.textboxfor(model => model.file, new { type = "file", id = "faqfile" })) <input type="submit" value="upload" /> }
hence phone call controller (note need add together foreach loop handle multiple files), more on below -
[httppost] public actionresult upload(uploadfilemodel filemodel) { const string path = @"c:\_temp\"; if (modelstate.isvalid) { var result = path.getfilename(filemodel.file.filename); if (result != null) { var sections = result.split('\\'); var filename = sections[sections.length - 1]; filemodel.file.saveas(path + filename); } } return redirecttoaction("index"); }
which calls model -
public class uploadfilemodel //: ienumerable { [filesize(10240)] [filetypes("jpg,jpeg,png,pdf")] public httppostedfilebase file { get; set; } //newly added after comments - add together here????????? public ienumerable<uploadfilemodel> uploadfilemodels { get; set; } }
my thought controller code should moved model reading online , in books.
question 1 - should code in controller moved model? because researching business logic should handled in model.
question 2 - since collection (i not strong on collections, striving master). a) add together class collection called uploadfilemodels strictly collection or build out uploadfilemodel inherent , handle collections , if how?
thank you.
question 1: code should moved model. purpose 2 fold. 1 makes code easier understand , read if business logic separated controller. 2. makes testing much easier if decouple business logic controller logic, yes recommend moving logic function on model.
question 2: not add together class collection. add together collection post method signature should like:
public actionresult upload(ienumerable<uploadfilemodel> filemodel)
and model class should be:
public class uploadfilemodel //: ienumerable { [filesize(10240)] [filetypes("jpg,jpeg,png,pdf")] public httppostedfilebase file { get; set; } }
and controller takes collection. side note model class stands right don't need @ all, use
public actionresult upload(ienumerable<httppostedfilebase> filemodel)
and buisness logic in controller repository model. not recommend doing this, because if want create changes controller makes much more difficult, worth noteing
c# asp.net asp.net-mvc
No comments:
Post a Comment