Mvc HttpPostedFileBase returns null - asp.net-mvc

lately i was trying to handle HttpPostedFileBase returns null issue, but i cannot really figure it out why my input file returns null although i've researched all the similiar questions to get to know about the problem in the site.My sutiation is i need to have four input file because i need four different documents other than each other.
here is part of the view:
#using (Html.BeginForm("Create", "MyController", FormMethod.Post, new { enctype ="multipart/form-data" }))
{
#Html.AntiForgeryToken()
<div style="padding-left: 32px;">
#Html.ValidationSummary(true)
#Html.HiddenFor(model => model.Id)
<table>
<tbody>
<tr>
<td style="width: 132px;"><div><b>...</b></div></td>
<td style="width:250px;">
<input type="file" name="upload1" id="upload1" class="formin" />
<input type="hidden" name="UploadType1" id="UploadType1" value="#Request.QueryString["UploadType"]" />
</td>
</tr>
<tr>
<td style="width: 132px;"><div><b>...</b></div></td>
<td style="width:250px;">
<input type="file" name="upload2" id="upload2" class="formin" />
<input type="hidden" name="UploadType2" id="UploadType2" value="#Request.QueryString["UploadType"]" />
</td>
</tr>
<tr>
<td style="width: 132px;"><div><b>...</b></div></td>
<td style="width:250px;">
<input type="file" name="upload3" id="upload3" class="formin" />
<input type="hidden" name="UploadType3" id="UploadType3" value="#Request.QueryString["UploadType"]" />
</td>
</tr>
<tr>
<td style="width: 132px;"><div><b>...</b></div></td>
<td style="width:250px;">
<input type="file" name="upload4" id="upload4" class="formin" />
<input type="hidden" name="UploadType4" id="UploadType4" value="#Request.QueryString["UploadType"]" />
</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td style="width:220px;"><div><b></b></div></td>
<td> <input type="submit" name="submit" value="GÖNDER" class="formbutton" style="font-size: 14px;background:maroon;color:white;"></td>
</tr>
</tbody>
</table>
here is my controller:
[HttpPost]
public ActionResult Create(HttpPostedFileBase upload1, HttpPostedFileBase upload2, HttpPostedFileBase upload3, HttpPostedFileBase upload4, string UploadType1, string UploadType2, string UploadType3, string UploadType4)
{
List<Ek> eks = new List<Ek>();
if (upload1!=null && upload1.ContentLength > 0)
{
var upload1Name= Path.GetFileName(upload1Name.FileName);
var path1 = Path.Combine(Server.MapPath("~/Documents/.../upload1"), PhotoName);
upload1.SaveAs(path1);
Ek e1 = new Ek {
UploadType = GetTypeEnum(UploadType1),
UploadPath = "/Documents/.../.../" + upload1Name
};
eks.Add(e1);
}
if (upload2!= null && upload2.ContentLength > 0)
{
var upload2Name = Path.GetFileName(upload2.FileName);
var path2 = Path.Combine(Server.MapPath("~/Documents/.../..."), upload2Name);
upload2.SaveAs(path2);
Ek e2 = new Ek
{
UploadType = GetTypeEnum(UploadType1),
UploadPath = "/Documents/.../.../" + upload2Name
};
eks.Add(e2);
}
if (upload3!= null && upload3.ContentLength > 0)
{
var upload3Name = Path.GetFileName(upload3.FileName);
var path3 = Path.Combine(Server.MapPath("~/Documents/.../..."), upload3Name );
upload3.SaveAs(path3);
Ek e3 = new Ek
{
UploadType = GetTypeEnum(UploadType1),
UploadPath = "/Documents/.../" + upload3Name
};
eks.Add(e3);
}
if (upload4!= null && upload4.ContentLength > 0)
{
var upload4Name = Path.GetFileName(upload4.FileName);
var path4 = Path.Combine(Server.MapPath("~/Documents/.../"), upload4Name);
languagepoint.SaveAs(path4);
Ek e4 = new Ek
{
UploadType = GetTypeEnum(UploadType1),
UploadPath = "/Documents/.../" + upload4Name
};
eks.Add(e4);
}
//some stuff here
return RedirectToAction("Success");
}
return View(myview);
}
private UploadType GetTypeEnum(string UploadType1)
{
Models.UploadType uType= UploadType.MyType;
switch (UploadType1)
{
case "MyType":
uType = UploadType.MyType;
break;
case "MyType1":
uType = UploadType.MyType1;
break;
case "MyType2":
uType = UploadType.MyType2;
break;
case "MyType3":
uType = UploadType.MyType3;
break;
default:
break;
}
return uType;
}
and finally here is my modal:
public partial class MyMainClass
{
public int Id { get; set; }
public virtual IList<Ek> Ek { get; set; }
}
public partial class Ek
{
public int ID { get; set; }
public UploadType UploadType { get; set; }
public string UploadPath { get; set; }
}
public enum UploadType
{
MyType= 0,
MyType1= 1,
MyType2= 2,
MyType3= 3
}
Thank you for all the answers.

Just Correct Your HttpPost Controller as :
[HttpPost]
public ActionResult Create(IEnumerable<HttpPostedFileBase> files)
{
if (files.Count() > 0) // display no of files uploaded
if (files.Any()) // display true
if (files.First() == null) // display "first null"
return View();
}
Because you are uploading more than one file from view so you have to use IEnumerable of HttpPostedFileBase and write HttpPost attribute on Create action because it is accepting your POST Request.

Related

MVC ModelState.IsValid not working

I am using below code to validate my model, but when I click on the 'Save Employee' button on the CreateEmployee view, the page is returning to CreateEmployee view without a validation messages.
Model
public class Employee
{
[Key]
public int EmployeeId { get; set; }
[Required(ErrorMessage="Enter First Name")]
public string FirstName { get; set; }
[Required(ErrorMessage="Enter Last Name")]
[StringLength(6,ErrorMessage="Last Name should not contain more than 6 characters")]
public string LastName { get; set; }
public int Salary { get; set; }
}
View
#{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
#*<meta name="viewport" content="width=device-width" />*#
<title>CreateEmployee</title>
<script type="text/javascript">
function ResetForm() {
document.getElementById("txtFirstName").value = "";
document.getElementById("txtLastName").value = "";
document.getElementById("txtSalary").value = "";
}
</script>
</head>
<body>
<div>
<form action="SaveEmployee" method="post">
#Html.ValidationSummary()
<table>
<tr>
<td>First Name:</td>
<td>
<input type="text" id="txtFirstName" name="FirstName" value="" /></td>
</tr>
<tr>
<td colspan="2" align="right">#Html.ValidationMessage("FirstName")</td>
</tr>
<tr>
<td>Last Name:</td>
<td>
<input type="text" id="txtLastName" name="LastName" value="" /></td>
</tr>
<tr>
<td colspan="2" align="right">#Html.ValidationMessage("LastName")
</td>
</tr>
<tr>
<td>
<input type="submit" value="SaveEmployee" />
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
Controller
public ActionResult SaveEmployee(Employee e,string BtnSubmit)
{
switch (BtnSubmit)
{
case "Save Employee":
if (ModelState.IsValid)
{
EmployeeBuisnessLayer objEBL = new EmployeeBuisnessLayer();
objEBL.SaveEmployee(e);
return RedirectToAction("Index");
}
else
{
return RedirectToAction("CreateEmployee");
}
case "Cancel":
return RedirectToAction("Index");
}
return new EmptyResult();
}
Don't redirect. You will lose modelstate. You need to let the method run to completion and pass the model back to the view.
Controller
[HttpPost]
public ActionResult CreateEmployee(Employee e, string BtnSubmit) {
switch (BtnSubmit) {
case "SaveEmployee":
if (ModelState.IsValid) {
EmployeeBuisnessLayer objEBL = new EmployeeBuisnessLayer();
objEBL.SaveEmployee(e);
return RedirectToAction("Index");
}
case "Cancel":
return RedirectToAction("Index");
default:
return new EmptyResult();
}
return View(e);
}
Your Form also needs to post to the CreateEmployee
View
<form action="CreateEmployee" method="post">

How to get client side generated class list to MVC post action argument?

I have one form where I am generating some control dynamically (Which is list some class . in image add product artwork section) When I click on submit button how can i get this values in post Action method's argument so that I can use this value in collection.
For reference I have attached the image where i have option for multiple Artwork oprion
[HttpPost]
public ActionResult Add(Graphic graphicToAdd,Enumerable<GraphicArtwork> artworkOption)
{
// I want value in artworkOption
}
public class GraphicArtwork
{
[Key]
public int GraphicArtworkId { get; set; }
public int GraphisId { get; set; }
[Required(ErrorMessage = "The {0} is required.")]
[DisplayName("Option Text")]
[StringLength(500)]
public string ArtOptionText { get; set; }
public decimal Price { get; set; }
[DisplayName("Active")]
public bool IsActive { get; set; }
public DateTime CreatedDate { get; set; }
[NotMapped]
public int TotalRecordCount { get; set; }
}
This is my view :
<div class="editor-field PrintOptions">
<table data-bind="visible :customArtworks().length > 0">
<thead>
<tr>
<td class="editor-label">Option</td>
<td class="editor-label">Price</td>
<td></td>
</tr>
</thead>
<tbody data-bind="foreach: customArtworks">
<tr>
<td>
<input placeholder="Enter Artwork Text" type="text" data-bind="value: ArtOptionText'}" />
</td>
<td>
<input placeholder="Enter Price" type="text" data-bind="value: Price" />
</td>
<td>
<img title="Add" src="~/Content/images/icon_add.png">
<img title="Delete" style="margin-left:10px;" src="~/Content/images/icon_delete.png">
</td>
</tr>
</tbody>
</table>
</div>
For more detail : I am generating my dynamical control using knockout.js
// Ko Implemntation
function GraphicArtwork(ArtOptionText, Price) {
var self = this;
self.ArtOptionText = ArtOptionText;
self.Price = Price;
self.formattedPrice = ko.computed(function () {
var price = self.Price;
return price ? "$" + price.toFixed(2) : "0.00";
})
}
function GraphicArtworkViewModel() {
var self = this;
self.customArtworks = ko.observableArray([GraphicArtwork(null, null)]);
self.add = function () {
self.customArtworks.push(new GraphicArtwork(null, null));
};
self.remove = function (GraphicArtwork) {
self.customArtworks.remove(GraphicArtwork);
};
}
ko.applyBindings(new GraphicArtworkViewModel());
Add This binding :
<td>
<input placeholder="Enter Artwork Text" type="text" data-bind="value: ArtOptionText,attr:{name: '['+$index()+'].ArtOptionText'}" />
</td>
<td>
<input placeholder="Enter Price" type="text" data-bind="value: Price,attr:{name: '['+$index()+'].Price'}" />
</td>
AND in Code Behind
public ActionResult Add(Graphic graphicToAdd,IEnumerable<GraphicArtwork> listOfGraphicArtwork)
{
// listOfGraphicArtwork will hold all the required data
}

fiil a list with values of a table

I'm new in learning asp.net MVC. I am writing because I am stubborn to a problem. Indeed, I have an application that should allow me to create an XML file that will be added to a database. At this point, I created my Model, and my view that allows me to create my XML tags.
I saw on this site that could add lines in my table via Javascript. What I have done just as you can see in the code.
I can not recover what is the value of each line that I can insert. Passing my view a list I created myself. I can recover both inputs I inserted in my controller.
My question is, there's another way to create a dynamic lines via javascript, then all the entries that the user has entered the recover and fill in my list? Then I know myself how I can play with my list. But I just want to recover all the different lines that my user has inserted. I am new in ASP.NET MVC. Any help , please
This is my code.
Model
public class XMLFile
{
public string TypeDoc { get; set; }
public string Type { get; set; }
public string Contenu { get; set; }
public string DocName { get; set; }
}
This is my controller :
public class XMLFileController : Controller
{
List<XMLFile> file = new List<XMLFile>();
[HttpGet]
public ActionResult Save()
{
file.AddRange( new XMLFile[] {
new XMLFile (){Type = "Titre", Contenu = "Chef de Service"},
new XMLFile (){Type = "Item", Contenu="Docteur Joel"}
});
return View(file);
}
[HttpPost]
public ActionResult Save(List<XMLFile> formCollection)
{
try
{
if (formCollection == null)
{
return Content("la liste est nulle");
}
else
{
return RedirectToAction("Create", "Layout");
}
}
catch
{
return View();
}
}
}
My view with a script for adding a new Row :
#using (Html.BeginForm("Save", "XMLFile", FormMethod.Post,new { #class = "form-horizontal", #role = "form", #id = "FormCreateXML" }))
{
<table class="table table-bordered" id="XMLFileTable">
<thead>
<tr>
<th>Type</th>
<th>Contenu</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
#for (int i = 0; i<Model.Count; i++)
{
<tr>
<td>#Html.TextBoxFor(model=>model[i].Type, new {#class="form-control help-inline", #placeholder="type" })</td>
<td> #Html.TextBoxFor(model=>model[i].Contenu, new {#class="form-control help-inline", #placeholder="contenu" })</td>
<td> <input type="button" class="BtnPlus" value="+" /> </td>
<td> <input type="button" class="BtnMinus" value="-" /> </td>
</tr>
}
</tbody>
<tfoot>
<tr>
<td> <button type="submit" class="btn btn-success" >Save</button> </td>
</tr>
</tfoot>
</table>
}
</body>
<script type="text/javascript">
$(document).ready(function () {
function addRow() {
var html = '<tr>' +
'<td><input type="text" class="form-control" placeholder="type"></td>' +
'<td> <input type="text" class="form-control" placeholder="contenu"></td>' +
'<td> <input type="button" class="BtnPlus" value="+" /> </td>' +
'<td> <input type="button" class="BtnMinus" value="-" /></td>' +
'</tr>'
$(html).appendTo($("#XMLFileTable"))
};
function deleteRow() {
var par = $(this).parent().parent();
par.remove();
};
$("#XMLFileTable").on("click", ".BtnPlus", addRow);
$("#XMLFileTable").on("click", ".BtnMinus", deleteRow);
});
</script>

When i set value of elements in editor template for grid pop up create , posting to controller as null

in my view there are two grid. when i select a row in first grid, second one is binding according to first one.
what i want to do is take common parameters from first one, used in second one create template in readonly or disabled inputs. my problem is input elements take parameter from first grid but, dont post to controller.
Controller Function
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult DonemKursSinifiOlustur([DataSourceRequest] DataSourceRequest request, DonemKursSinifi model,string DonemId, string DersId, string EgitmenId )
{
if (model != null && ModelState.IsValid)
{
Helper.Islemci.DonemKursSinifiTanimla(model);
}
return Json(new[] { model }.ToDataSourceResult(request, ModelState));
}
model.DonemId, model.DersId, model.EgitmenId and DonemId, DersId, EgitmenId come null.
EditorTemplate View for Grid Create and Update
#model Kurslar.Models.DonemKursSinifi
#using (Html.BeginForm("DonemKursSinifiOlustur","Tanim",FormMethod.Post))
{
<table>
<tr>
<td>
Lütfen Gün ve Saati Belirtiniz:
</td>
<td>
#Html.Kendo().AutoCompleteFor(m=>m.Tanim)
</td>
</tr>
<tr>
<td>
Donem :
</td>
<td>
#Html.Kendo().AutoCompleteFor(m=>m.DonemBaslangicBitis)
#Html.HiddenFor(m => m.DonemId)
</td>
</tr>
<tr>
<td>
Ders Adı:
</td>
<td>
#Html.Kendo().AutoCompleteFor(m=>m.DersAdi)
#Html.HiddenFor(m => m.DersId)
</td>
</tr>
<tr>
<td>
Eğitmen
</td>
<td>
#Html.Kendo().AutoCompleteFor(m=>m.EgitmenAdiSoyadi)
#Html.HiddenFor(m => m.DonemId)
</td>
</tr>
</table>}
First AutoCompleteFor works correctly because take input from user, not before setted.
*and my javaScript code to fill parameters to EditorTemplate *
and it works fine
var grid = $("#donemGrid").data("kendoGrid");
var rows = grid.select();
alert(rows);
try {
var donemID = grid.dataItem(rows).DonemId;
var dersID = grid.dataItem(rows).DersId;
var egitmenID = grid.dataItem(rows).EgitmenId;
var dersAdi = grid.dataItem(rows).DersAdi;
var egitmenAdiSoyadi= grid.dataItem(rows).EgitmenAdiSoyadi;
var donemBaslangicBitis = grid.dataItem(rows).DonemBaslangicBitis;
} catch (e) {
alert(e);
}
$("#DonemBaslangicBitis").data("kendoAutoComplete").value(donemBaslangicBitis);
$("#DersAdi").data("kendoAutoComplete").value(dersAdi);
$("#EgitmenAdiSoyadi").data("kendoAutoComplete").value(egitmenAdiSoyadi);
$("#DonemId").val(donemID);
$("#DersId").val(dersID);
$("#EgitmenId").val(egitmenID);
*if needed, my model *
public class DonemKursSinifi
{
[Key]
[Required]
[PersistentProperty(IsAutoIncremented = true)]
public int Id { get; set; }
[PersistentProperty]
public string Tanim { get; set; }
[PersistentProperty]
public int DonemId { get; set; }
[PersistentProperty]
public int DersId { get; set; }
[PersistentProperty]
public int EgitmenId { get; set; }
[PersistentProperty]
public int KontenjanSayisi { get; set; }
[PersistentProperty]
public int TarifeId { get; set; }
[PersistentProperty]
public int IslemNo { get; set; } // default 1
public string EgitmenAdiSoyadi { get; set; }
public string DersAdi { get; set; }
public string DonemBaslangicBitis { get; set; }
}
ok, probably you have repeated the id in the grid and also have the same name attributes in the same form to do this:
#Html.HiddenFor(m => m.DersId)
mabe you can do somethin like this:
form:
#model Kurslar.Models.DonemKursSinifi
#using (Html.BeginForm("DonemKursSinifiOlustur","Tanim", FormMethod.Post, new { id="myform"}))
{
<input type="hidden" value="" name="Tanim" />
<input type="hidden" value="" name="DonemBaslangicBitis" />
<input type="hidden" value="" name="DonemId" />
<input type="hidden" value="" name="DersAdi" />
<input type="hidden" value="" name="DersId" />
<input type="hidden" value="" name="EgitmenAdiSoyadi" />
<input type="hidden" value="" name="DonemId" />
}
table:
<table>
<tr>
<td>Lütfen Gün ve Saati Belirtiniz:</td>
<td>#Html.Kendo().AutoCompleteFor(m=>m.Tanim)</td>
</tr>
<tr>
<td>Donem :</td>
<td>#Html.Kendo().AutoCompleteFor(m=>m.DonemBaslangicBitis) #Html.HiddenFor(m => m.DonemId)</td>
</tr>
<tr>
<td>Ders Adı:</td>
<td>#Html.Kendo().AutoCompleteFor(m=>m.DersAdi) #Html.HiddenFor(m => m.DersId)</td>
</tr>
<tr>
<td>Eğitmen</td>
<td>#Html.Kendo().AutoCompleteFor(m=>m.EgitmenAdiSoyadi) #Html.HiddenFor(m => m.DonemId)</td>
</tr>
</table>
js:
var
grid = $("#donemGrid").data("kendoGrid"),
rows = grid.select(),
form = $('#myform');
form.find('input[name="DonemBaslangicBitis"]').val(grid.dataItem(rows).DonemBaslangicBitis);
form.find('input[name="DersAdi"]').val(grid.dataItem(rows).DersAdi);
form.find('input[name="EgitmenAdiSoyadi"]').val(grid.dataItem(rows).EgitmenAdiSoyadi);
form.find('input[name="DonemId"]').val(grid.dataItem(rows).DonemId);
form.find('input[name="DersId"]').val(grid.dataItem(rows).DersId);
form.find('input[name="EgitmenId"]').val(grid.dataItem(rows).EgitmenId);
form.submit();

error to get value from controller

i try to create new room, but roomTypeID always return 1, whats wrong with my code?
i can make a new room type, but i cant insert room facility in my database, because RoomType ID always return 1
this my code..
my controller
public ActionResult NewRoom()
{
ViewBag.hotel = _hotelService.GetByID(_HotelID).HotelName;
List<ShowEditRoomViewModel> showEditRoomViewModel = _roomTypeService.showNewRooms();
return View(showEditRoomViewModel.FirstOrDefault());
}
[HttpPost]
public ActionResult NewRoom(FormCollection typeRoom)
{
_roomTypeService.NewRoom(_HotelID, typeRoom["RoomTypeName"], typeRoom["RoomTypeDescription"]);
List<string> IDs = typeRoom["FacilityIDs"].Split(',').ToList();
List<int> FacilityIDs = new List<int>();
foreach (string ID in IDs)
{
FacilityIDs.Add(Convert.ToInt32(ID));
}
_roomTypeService.UpdateFacilityInRooms(FacilityIDs, Convert.ToInt32(typeRoom["RoomTypeID"]));
return NewRoom();
}
my service
public void UpdateFacilityInRooms(List<int> FacilityIDs, int RoomTypeID)
{
List<HotelRoomFacility> hotelRoomFacilities = _HotelRoomFacilityRopository.AsQueryable().Where(f => f.RoomTypeID == RoomTypeID).ToList();
foreach (int newRoomFacility in FacilityIDs)
{
if (hotelRoomFacilities.Where(h => h.RoomFacilityID == newRoomFacility).Count() == 0)
{
HotelRoomFacility facility = new HotelRoomFacility
{
RoomFacilityID = newRoomFacility,
RoomTypeID = RoomTypeID
};
_HotelRoomFacilityRopository.Add(facility);
}
}
_HotelRoomFacilityRopository.CommitChanges();
}
my view model
public class ShowEditRoomViewModel
{
public int RoomTypeID { get; set; }
public string RoomTypeName { get; set; }
public string RoomTypeDescription { get; set; }
public List<FaciliyInRoom> facilityinRoom { get; set; }
}
my view
#model XNet.Repository.Model.ShowEditRoomViewModel
#{
ViewBag.Title = "NewRoom";
}
<h2>New Room</h2>
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<fieldset>
<legend>Isikan Data</legend>
<div>
#Html.Label("Hotel Name")
</div>
<div>
#ViewBag.hotel
</div>
<br />
<div>
#Html.HiddenFor(model => model.RoomTypeID)
</div>
<br />
<div>
#Html.Label("Room Type Name")
</div>
<div>
#Html.EditorFor(model => model.RoomTypeName)
#Html.ValidationMessageFor(model => model.RoomTypeName)
</div>
<br />
<div>
#Html.Label("Room Type Description")
</div>
<div>
#Html.TextAreaFor(model => model.RoomTypeDescription)
#Html.ValidationMessageFor(model => model.RoomTypeDescription)
</div>
<br />
<table>
<thead>
<tr>
<th>Facility Name</th>
<th> is available</th>
</tr>
</thead>
<tbody>
#foreach (var facility in Model.facilitiesInRoom)
{
<tr>
<td>
#(facility.RoomFacilityName)
</td>
<td style="text-align:center;">
<input type="checkbox" #(facility.RoomFacilityAvailable ? " checked=checked" : null) name="FacilityIDs" value="#facility.RoomFacilityID" />
</td>
</tr>
}
</tbody>
</table>
<br />
<p>
<input type="submit" value="Save" />
<input style="width:100px;" type="button" title="EditHotelDetail" value="Back to Detail" onclick="location.href='#Url.Action("Room", "Hotel") '" />
</p>
</fieldset>
}
My method
public List<ShowEditRoomViewModel> showNewRooms()
{
List<RoomType> roomTypes = (from d in _RoomTypeRepository.All()
select d).ToList();
List<ShowEditRoomViewModel> showEditRoomViewModel = new List<ShowEditRoomViewModel>();
foreach (RoomType roomType in roomTypes)
{
showEditRoomViewModel.Add(new ShowEditRoomViewModel
{
RoomTypeID = roomType.RoomTypeID,
facilitiesInRoom = LoadFacilityInRoom()
});
}
return showEditRoomViewModel;
}
can someone tell me, where is my mistake??
thanks
When you are inserting RoomtypeId in Database, you are using ExecuteNonQuery() method, It will always return 1 whenever you insert a new record in it,
If you are using stored procedure for inserting,you can use
select Scope_identity()
after insertion.

Resources