asp.net mvc - MVC 4 How to call Upload function In Extisting Controller -


here model :

public string altclc { get; set; }  public string altgen { get; set; } public int altcnic { get; set; } public datetime altdob { get; set; } [required]  public string altname { get; set; } 

here controller name = adultliteracy controller every thing ef write class name upload not working

namespace literacypayroll.controllers { public class adultliteracyteacherscontroller : controller { private payrolldbcontext db = new payrolldbcontext();  // // get: /adultliteracyteachers/  public actionresult index() { var adulliteracyteachers = db.adulliteracyteachers.include(a => a.district); return view(adulliteracyteachers.tolist()); } [httppost] public actionresult upload(httppostedfilebase[] files) { foreach (httppostedfilebase file in files) { string path = system.io.path.combine(server.mappath("~/app_data"), system.io.path.getfilename(file.filename)); file.saveas(path); } viewbag.message = "file(s) uploaded successfully"; return redirecttoaction("index"); } // // get: /adultliteracyteachers/details/5  public actionresult details(int id = 0) { adulliteracyteachers adulliteracyteachers = db.adulliteracyteachers.find(id); if (adulliteracyteachers == null) { return httpnotfound(); } return view(adulliteracyteachers); }  / 

here view :

    @model literacypayroll.models.adulliteracyteachers      @{     viewbag.title = "create";     }      <h2>create</h2>      @using (html.beginform()) {     @html.validationsummary(true)      <fieldset>     <legend>adulliteracyteachers</legend>      <div class="editor-label">     @html.labelfor(model => model.distid, "district")     </div>     <div class="editor-field">     @html.dropdownlist("distid", string.empty)     @html.validationmessagefor(model => model.distid)     </div>      <div class="editor-label">     @html.labelfor(model => model.altclc)     </div>     <div class="editor-field">     @html.editorfor(model => model.altclc)     @html.validationmessagefor(model => model.altclc)     </div>      <div class="editor-label">     @html.labelfor(model => model.altgen)     </div>     <div class="editor-field">     @html.editorfor(model => model.altgen)     @html.validationmessagefor(model => model.altgen)     </div>      <div class="editor-label">     @html.labelfor(model => model.altcnic)     </div>     <div class="editor-field">     @html.editorfor(model => model.altcnic)     @html.validationmessagefor(model => model.altcnic)     </div>      <div class="editor-label">     @html.labelfor(model => model.altdob)     </div>     <div class="editor-field">     @html.editorfor(model => model.altdob)     @html.validationmessagefor(model => model.altdob)     </div>      <div class="editor-label">     @html.labelfor(model => model.altname)     </div>   <label for="file">upload image:</label> <input type="file" name="files" value="upload image" /> <input name="upload" type="submit" value="create" /> </p> </fieldset> 

the problem facing when press create button values pass database image file not posted how call upload action result within existing controller class ?

for file posting in form, have make form this:

@using (html.beginform("action", "controller", formmethod.post, new { enctype="multipart/form-data"})){ ...... ..... } 

and in action:

[httppost]  public actionresult action(adulliteracyteachers model, httppostedfilebase file) { } 

or can read file request:

[httppost]     public actionresult action(adulliteracyteachers model)    {        foreach (string requestfile in request.files)                 {                     httppostedfilebase file = request.files[requestfile];                     if (file.contentlength > 0)                     {                     }                 }     } 

Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -