question

amnaw-7277 avatar image
0 Votes"
amnaw-7277 asked amnaw-7277 commented

the form Http POST doesn't work, only Get work in asp core mvc

I have a Form that should pass data through POST request, but GET request is being used without passing the data and do model binding of asp core, so buz of that the method Registrationp is never reach if the attribute [HttpPost] is in place ;( . I tried many ways to get over this problem but none if them worked, even though the other forms post the data and bind the model successfully

HTML:

 @model Student_Training.Models.File;
    
 <form method="post" asp-controller="Students" asp-action="Registrationp" enctype="multipart/form-data">
     <label asp-for="FileRep" class="col-sm-2 col-form-label"></label>
     <div class="col-sm-10">
         <div class="custom-file">
             <input type="file" asp-for="FileRep" name="img" accept="image/*" class="form-control custom-file-input" />
             <label class="custom-file-label">Upload..</label>
         </div>
     </div>
     <div class="form-group">
         <button id="Button" type="submit" name="submit" formmethod="post" class="btn btn-secondary btn-sm">
             <a asp-action="Registrationp">Save</a>
         </button>
     </div>
 </form>

Controller POST method:


 [HttpPost]
         public async Task<IActionResult> Registrationp([Bind("FileId, FileName, OwnerId, FileRep")] Student_Training.Models.File imgFileModel, IFormFile img, int? id)
         {
             var user = await _userManager.FindByNameAsync(User.Identity.Name);
             var userEmail = user.Email;
             Student Student = _context.Student.FirstOrDefaultAsync(x => x.Email == userEmail).Result;
             // StudentId
             id = Student.StudentId;
    
             // Save img to wwwroot/img folder
             string wwwRootPath = _hostEnvironment.WebRootPath;
             Student_Training.Models.File myFile = new Student_Training.Models.File();
             string fileName = Path.GetFileNameWithoutExtension(imgFileModel.FileRep.FileName);
             string extention = Path.GetExtension(imgFileModel.FileRep.FileName);
             imgFileModel.FileName = fileName = fileName + DateTime.Now.ToString("yymmss") + extention;
             string path = Path.Combine(wwwRootPath + "/img/", fileName);
             using (var fileStream = new FileStream(path, FileMode.Create))
             {
                 await imgFileModel.FileRep.CopyToAsync(fileStream);
             }
             // insert recorod to Db
             _context.Add(imgFileModel);
             await _context.SaveChangesAsync();
    
             return View();
         }






dotnet-aspnet-core-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RenaNi-MSFT avatar image
0 Votes"
RenaNi-MSFT answered amnaw-7277 commented

Hi @amnaw-7277 ,

You need change:

 <button id="Button" type="submit" name="submit" formmethod="post" class="btn btn-secondary btn-sm"><a asp-action="Registrationp">Save</a></button>

To:

 <button id="Button" type="submit" name="submit" formmethod="post" class="btn btn-secondary btn-sm">Save</button>

If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Best Regards,

Rena


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you :)

0 Votes 0 ·
DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered

Your binding indicate you are trying to bind on four fields, but I only see one field in the view.

Maybe, you should better understand how bindings work.

https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-5.0

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.