During View data not getting captured in the Controller in ASP.Net Core 2.0 MVC. I am new to ASP.Net core MVC - asp.net-core

I am trying to implement Update/Edit functionality in ASP.Net Core using Razor. Below is the view page.
After clicking Submit button the view page is POSTed to the "GetCustomerForEdit" method of the controller as mentioned below in the code. But unfortunately the edited data from the view is not transferred to the controller method "GetCustomerForEdit", parameter "EditCustomer" of type "GetAllCustDetails".
Since i am new to ASP.net Core is there something i am missing or not doing right in the View or Controller?
VIEW CODE:
<form asp-action="GetCustomerForEdit">
<div>
<table class="tables1">
#foreach (var item in Model.Custs)
{
<tr>
<th style="width:300px;" colspan="2">Update Customers
Details</th>
</tr>
<tr style="visibility:hidden">
<td><input asp-for="#item.CustomerId" type="hidden" />/td>
</tr>
<tr>
<td>First Name</td>
<td><input asp-for="#item.FirstName" autofocus
class="textbox1" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input asp-for="#item.LastName" class="textbox1" />
</td>
</tr>
<tr>
<td>Sex</td>
<td><select asp-for="#item.Sex" class="textbox1" asp-
items="#(new SelectList(ViewBag.listofitems, "Sex", "Sex"))"></select>
</td>
</tr>
<tr>
<td>Age</td>
<td><input asp-for="#item.Age" class="textbox1"
type="text" /></td>
</tr>
<tr>
<td style="width: 177px">Address1</td>
<td><input asp-for="#item.Address1" class="textbox1"
type="text" /></td>
</tr>
<tr>
<td>Address2</td>
<td><input asp-for="#item.Address2" class="textbox1"
type="text" /></td>
</tr>
<tr>
<td>State</td>
<td><input asp-for="#item.State" class="textbox1"
type="text" /></td>
</tr>
<tr>
<td>City</td>
<td><input asp-for="#item.City" class="textbox1"
type="text" /></td>
</tr>
<tr>
<td>Pin</td>
<td><input asp-for="#item.Pin" class="textbox1"
type="text" /></td>
</tr>
<tr>
<td>Phone</td>
<td><input asp-for="#item.Phone" class="textbox1"
type="text" /></td>
</tr>
<tr>
<td>Mobile</td>
<td><input asp-for="#item.Mobile" class="textbox1"
type="text" /></td>
</tr>
<tr>
<td>Email</td>
<td><input asp-for="#item.Email" class="textbox1"
type="text" /></td>
</tr>
}
<tr class="trfooter">
<td colspan="2">
<input class="submit" type="submit" value="Save" />
<input type="reset" class="submit" value="Reset" />
<input type="button" class="submit" value="Cancel" />
</td>
</tr>
</table>
#if (TempData["RecordSave"] != null)
{
<p>#TempData["RecordSave"]</p>
}
</div>
</form>
MODEL CODE:
public class GetAllCustDetails
{
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Customer> Custs;
}
Where Customer model:
public class Customer
{
public int CustomerId { get; set; }
[Required(ErrorMessage =" Please enter First Name!")]
public string FirstName { get; set; }
[Required(ErrorMessage =" Please enter Last Name!")]
public string LastName { get; set; }
[Required(ErrorMessage =" Please enter Sex!")]
public string Sex { get; set; }
[Range(0,99)]
[Required(ErrorMessage =" Please enter valid Age!")]
public int Age { get; set; }
[Required(ErrorMessage =" Please enter valid Adddress!")]
public string Address1 { get; set; }
public string Address2 { get; set; }
[Required(ErrorMessage =" Please enter State!")]
public string State { get; set; }
[Required(ErrorMessage =" Please enter valid State!")]
public string City { get; set; }
[Required(ErrorMessage =" Please enter valid Pin!")]
public int Pin { set; get; }
public long Phone { set; get; }
public long Mobile { get; set; }
[EmailAddress(ErrorMessage =" Please enter valid Email!")]
[Required(ErrorMessage = "Email is required field!")]
public string Email { get; set; }
}
CONTROLLER CODE: [HttpGet is working fine]
[HttpGet]
public IActionResult GetCustomerForEdit(Int16? Id)
{
GetAllCustDetails getCustForEdit;
clsCustomerDAL getCust;
getCust = new clsCustomerDAL();
getCustForEdit = getCust.GetCustomerForUpdate(Id);
SexDropDown();// this is dropdown getting populated from DB
return View(getCustForEdit);
}
[HttpPost]
public IActionResult GetCustomerForEdit([Bind] GetAllCustDetails
EditCustomer)
{
bool retVal = false;
int CustId = 0;
string Fname;
string Lname;
if(ModelState.IsValid)
{
clsCustomerDAL updateCust = new clsCustomerDAL();
retVal = updateCust.DALEditCustomer(EditCustomer);
if(retVal == true)
{
ModelState.Clear();
}
return View("getCustomerDetails");
}
return View("IndexPage");
}

Since you need to pass a List of Customer from View to controller, for model binding, you could specify name property for each input.
Edit view:
#model GetAllCustDetails
<form asp-action="GetCustomerForEdit">
<div>
<table class="tables1">
#{int i = 0;}
#foreach (var item in Model.Custs)
{
<tr>
<th style="width:300px;" colspan="2">
Update Customers
Details
</th>
</tr>
<tr style="visibility:hidden">
<td><input asp-for="#item.CustomerId" type="hidden" name="Custs[#i].CustomerId" />/td>
</tr>
<tr>
<td>First Name</td>
<td>
<input asp-for="#item.FirstName" name="Custs[#i].FirstName" autofocus class="textbox1" />
</td>
</tr>
//other <tr> properties
<tr>
<td>Email</td>
<td>
<input asp-for="#item.Email" name="Custs[#i].Email" class="textbox1" type="text" />
</td>
</tr>
i++;
}
<tr class="trfooter">
<td colspan="2">
<input class="submit" type="submit" value="Save" />
<input type="reset" class="submit" value="Reset" />
<input type="button" class="submit" value="Cancel" />
</td>
</tr>
</table>
#if (TempData["RecordSave"] != null)
{
<p>#TempData["RecordSave"]</p>
}
</div>
</form>
Post action:
[HttpPost]
public IActionResult GetCustomerForEdit([Bind] GetAllCustDetails EditCustomer)

Related

How to show contents from other Models in my view using .NET Core 6?

I have an Index view that should display the name of each person's course, however, in the model used there is only the Course Id, the course name is in another model - CursosModel - and in another table in the database. How can I display the course name in my view?
Index view:
#model IEnumerable<FeedbackUnivicosa.Models.Professor>
#{
ViewData["Title"] = "Professores";
}
<h4>#ViewData["Title"]</h4>
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
<input type="text" name="SearchString" class="inputSearch" value="#ViewData["CurrentFilter"]" />
<button type="submit" class="botaoSearch fa fa-solid fa-magnifying-glass"></button>
<a asp-action="Index"><i class="espacoFiltro fa fa-solid fa-filter-circle-xmark"></i></a>
<a asp-action="Create" class="botaoCadastro btn btn-primary btn-sm">Cadastrar</a>
</p>
</div>
</form>
<table class="table">
<thead>
<tr>
<th></th>
<th>
<a asp-action="Index" asp-route-sortOrder="#ViewData["NameSortParm"]">Professor</a>
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="#ViewData["EmailSortParm"]">Email</a>
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="#ViewData["CursoSortParm"]">Curso</a>
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model) {
<tr>
<td class="fa-lg">
<a asp-action="Edit" asp-route-id="#item.Id"><i class="espacoIcone fa-solid fa-user-pen"></i></a>
<a asp-action="Delete" asp-route-id="#item.Id"><i class="espacoIcone fa-solid fa-trash-can"></i></a>
</td>
<td>
#Html.DisplayFor(modelItem => item.Nome)
</td>
<td>
#Html.DisplayFor(modelItem => item.EmailProfessor)
</td>
<td>
#Html.DisplayFor(modelItem => item.CursoId)
</td>
</tr>
}
</tbody>
</table>
I am trying to show the course name on the Index view.
From your description of this question, I think you can inject your DBContext into your view and get the value of course name from CursosModel table, Please refer to this simple demo:
Model:
public class CursosModel
{
public int id { get; set; }
public string CursoName { get; set; }
}
public class Professor
{
public int Id { get; set; }
public string Name { get; set; }
public int CourseId { get; set; }
}
Then in the view, You can inject dbcontext into your view then get the course Name by Professor's CourseId.
#using xxxx;
#model IEnumerable<Professor>
#inject ApplicationDbContext dbcontext
#foreach (var item in Model)
{
<tr>
<td class="fa-lg">
<a asp-action="Edit" asp-route-id="#item.Id"><i class="espacoIcone fa-solid fa-user-pen"></i></a>
<a asp-action="Delete" asp-route-id="#item.Id"><i class="espacoIcone fa-solid fa-trash-can"></i></a>
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.CourseId)
</td>
<td>
#dbcontext.cursosModels.Where(x => x.id == item.CourseId).Select(y => y.CursoName).FirstOrDefault();
</td>
<br>
</tr>
}
Demo:
because the Microsoft website teaches Add the model
so after you can reference the Microsoft site.
reference Microsoft website
public class CursosModel
{
public List<CursosModel> CursosModel { get; set; }
public List<Course> Course{ get; set; }
}

How to get a value from a dynamic form?

I am using .NET Core with razor pages and I have a model as a list of object as below. Can anyone tell me how to do it? I searched many forums, seems only using controller can do it.
Here is the razor cshtml form I'm using:
<form id="form" method="post" data-ajax="true" data-ajax-method="post" >
<h4>Performance Rating</h4>
<div>
<p>
<mark>
#Model.CommonContents.Find(x => x.ContentId.Equals(24)).ContentText
</mark>
</p>
</div>
<div class="table-responsive">
<table id="contentTbl" class="display nowrap table table-striped table-bordered">
<thead>
<tr>
<th width="25%">
#Model.CommonContents.Find(x => x.ContentId.Equals(25)).ContentText
</th>
<th style="font-size:xx-small" width="5%">(a)<br /> #Model.CommonContents.Find(x => x.ContentId.Equals(26)).ContentText</th>
<th style="font-size:xx-small" width="5%">(b) <br />#Model.CommonContents.Find(x => x.ContentId.Equals(27)).ContentText</th>
<th style="font-size:xx-small" width="5%">(c)=(a)X(b)<br />#Model.CommonContents.Find(x => x.ContentId.Equals(28)).ContentText</th>
<th>#Model.CommonContents.Find(x => x.ContentId.Equals(29)).ContentText</th>
<th width="5%">#Model.CommonContents.Find(x => x.ContentId.Equals(30)).ContentText</th>
</tr>
</thead>
<tbody>
#{ var j = 0; }
#{ var QuestionId = 0;}
#for (int k = 0; k < Model.QuesAns.Count; k++)
{
j = k + 1;
QuestionId = Model.QuesAns[k].QuestionId;
<tr>
<td>
<h5>#j. #Html.DisplayFor(modelitem => Model.QuesAns[k].TitleText)</h5>
<p>#Html.DisplayFor(modelitem => Model.QuesAns[k].ContentText)</p>
<input type="hidden" name="QuestionId_#j" , id="QuestionId_#j" value="#QuestionId" />
</td>
<td align="center">
<lable id="Weighting_#j">#Html.DisplayFor(modelitem => Model.QuesAns[k].Weighting)</lable>%
</td>
<td align="center">
<select class="form-control p-0" name="Weighting_#j" id="Weighting_#j" asp-for="#Model.QuesAns[k].Weighting">
<option value="0"></option>
<option value="5">5</option>
<option value="4.5">4.5</option>
<option value="4">4</option>
<option value="3.5">3.5</option>
<option value="3">3</option>
<option value="2.5">2.5</option>
<option value="2">2</option>
<option value="1.5">1.5</option>
<option value="1">1</option>
</select>
</td>
<td align="center"><lable id="WeightingMarks_#j"></lable></td>
<td>
<textarea class="form-control" rows="10" name="Comment_#j" id="Comment_#j" asp-for="#Model.QuesAns[k].Answer" ></textarea>
</td>
<td>
(#Html.DisplayFor(modelitem => Model.QuesAns[k].AttachNum))
<span class="k-icon k-i-attachment-45"></span>
</td>
</tr>
}
<tr style="background-color:burlywood">
<td align="right">
<input type="hidden" value="#Model.QuesAns.Count" name="TotalQues" id="TotalQues" />
<b>
#Model.CommonContents.Find(x => x.ContentId.Equals(31)).ContentText
</b>
</td>
<td>100%</td>
<td></td>
<td><label id="TotalWeightingMarks"></label></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>
<div class="row">
<input type="text" id="formAction" name="formAction" style="display:none">
<input id="btnSubmit" type="submit" value="Confirmed" class="btn btn-block btn-danger" />
</div>
</div>
</form>
javascript part
$(document).ready(function () {
$('#btnSubmit').click(function () {
$('#formAction').val("submit");
}); });
the coding page like this
[BindProperty]
public List<VAppraisalQuestionAnswer> qa { set; get; }
public IActionResult OnPost(string formAction)
{
//i want to get value from the from
var abc = qa; // qa is return count= 0
return Page();
}
Here is the model
public class VAppraisalQuestionAnswer
{
[StringLength(50)]
public string StaffCode { get; set; }
[StringLength(50)]
public string DeptCode { get; set; }
[StringLength(10)]
public string AppraisalPeriodCode { get; set; }
public int QuestionId { get; set; }
[StringLength(int.MaxValue)]
public string TitleText { get; set; }
[StringLength(int.MaxValue)]
public string ContentText { get; set; }
public int Weighting { get; set; }
public string Answer { get; set; }
public int AttachNum { get; set; }
public int DspOrd { get; set; }
}
I already found the solution. All I need to do is add IFormCollection to the OnPostAsync, then IFormCollection returns the array value.
like this:
public async Task<IActionResult> OnPostAsync(IFormCollection Form) {
To bind a simple type list object, the name attribute in the input text should in this format: [index].property. The index must start from 0 and be continuous. Change the table body like below:
<tbody>
#{ var j = 0; }
#{ var QuestionId = 0;}
#for (int k = 0; k < Model.QuesAns.Count; k++)
{
j = k + 1;
QuestionId = Model.QuesAns[k].QuestionId;
<tr>
<td>
<h5>#j. #Html.DisplayFor(modelitem => Model.QuesAns[k].TitleText)</h5>
<p>#Html.DisplayFor(modelitem => Model.QuesAns[k].ContentText)</p>
<input type="hidden" name="[#k].QuestionId" , id="QuestionId_#j" value="#QuestionId" />
</td>
<td align="center">
<lable id="Weighting_#j">#Html.DisplayFor(modelitem => Model.QuesAns[k].Weighting)</lable>%
</td>
<td align="center">
<select class="form-control p-0" name="[#k].Weighting" id="Weighting_#j" asp-for="#Model.QuesAns[k].Weighting">
<option value="0"></option>
<option value="5">5</option>
<option value="4.5">4.5</option>
<option value="4">4</option>
<option value="3.5">3.5</option>
<option value="3">3</option>
<option value="2.5">2.5</option>
<option value="2">2</option>
<option value="1.5">1.5</option>
<option value="1">1</option>
</select>
</td>
<td align="center"><lable id="WeightingMarks_#j"></lable></td>
<td>
<textarea class="form-control" rows="10" name="[#k].Comment" id="Comment_#j" asp-for="#Model.QuesAns[k].Answer"></textarea>
</td>
<td>
(#Html.DisplayFor(modelitem => Model.QuesAns[k].AttachNum))
<span class="k-icon k-i-attachment-45"></span>
</td>
</tr>
}
<tr style="background-color:burlywood">
<td align="right">
<input type="hidden" value="#Model.QuesAns.Count" name="TotalQues" id="TotalQues" />
</td>
<td>100%</td>
<td></td>
<td><label id="TotalWeightingMarks"></label></td>
<td></td>
<td></td>
</tr>
</tbody>

Can not send list of objects from view to controller using tag helper in asp.net core 2.2

I have the following view and I want to send list of object from view to controller and I used asp-for tag helper for data binding but action in controller receives null
#model IEnumerable<GoodsList>
<form method="post" asp-action="SubmitList" asp-controller="Submit">
<table class="table table-bordered">
<thead>
<tr>
<th width="2%">number</th>
<th width="20%">Name</th>
<th width="20%">Brand</th>
<th width="20%">Quantity</th>
<th width="20%">Scale</th>
<th width="8%">operation</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>#item.Number</td>
<td><input type="text" readonly="readonly" asp-for="#item.GoodsName" class="form-control" /></td>
<td><input type="text" readonly="readonly" asp-for="#item.BrandName" class="form-control" /></td>
<td><input type="text" readonly="readonly" asp-for="#item.Quantity" class="form-control" /></td>
<td><input type="text" readonly="readonly" asp-for="#item.ScaleName" class="form-control" /></td>
<td>
<select class="form-control" asp-items="#(new SelectList(item.Status,"Id","Name"))">
</select>
</td>
</tr>
}
<tr>
<td colspan="6">
<textarea class="form-control" rows="3" readonly="readonly" cols="5">#Model.Select(s => s.Description).First()</textarea>
</td>
</tr>
</tbody>
</table>
<a class="btn btn-primary">Back</a>
<input type="submit" value="Submit" class="btn btn-success" style="width:auto">
</form>
And Here is my controller which receives null
[HttpPost]
//It receives null
public IActionResult SubmitList(IEnumerable<GoodsList> model)
{
return View();
}
And the Model
public class GoodsList
{
public GoodsList()
{
Status = new List<ApprovalStatus>();
}
public int Number { get; set; }
public string GoodsName { get; set; }
public string BrandName { get; set; }
public int? Quantity { get; set; }
public string UserName { get; set; }
public string RankName { get; set; }
public int? RequestId { get; set; }
public string ScaleName { get; set; }
public IList<ApprovalStatus> Status { get; set; }
}
Any solution?
Thanks in advance
Change your razor view like below:
#model IEnumerable<GoodsList>
<form method="post" asp-action="SubmitList" asp-controller="Submit">
<table class="table table-bordered">
<thead>
<tr>
<th width="2%">number</th>
<th width="20%">Name</th>
<th width="20%">Brand</th>
<th width="20%">Quantity</th>
<th width="20%">Scale</th>
<th width="8%">operation</th>
</tr>
</thead>
<tbody>
#{ var i = 0;}
#foreach (var item in Model)
{
<tr>
<td>#item.Number</td>
<td><input type="text" readonly="readonly" name="[#i].GoodsName" asp-for="#item.GoodsName" class="form-control" /></td>
<td><input type="text" readonly="readonly" name="[#i].BrandName" asp-for="#item.BrandName" class="form-control" /></td>
<td><input type="text" readonly="readonly" name="[#i].Quantity" asp-for="#item.Quantity" class="form-control" /></td>
<td><input type="text" readonly="readonly" name="[#i].ScaleName" asp-for="#item.ScaleName" class="form-control" /></td>
<td>
<select class="form-control" name="[#i].Status[0].Id" asp-items="#(new SelectList(item.Status,"Id","Name"))">
</select>
</td>
</tr>
i++;
}
<tr>
<td colspan="6">
<textarea class="form-control" rows="3" readonly="readonly" cols="5">#Model.Select(s => s.Description).First()</textarea>
</td>
</tr>
</tbody>
</table>
<a class="btn btn-primary">Back</a>
<input type="submit" value="Submit" class="btn btn-success" style="width:auto">
</form>
Result:
Another way:
#model IList<GoodsList> //change this
//..
<tbody>
#for(var i = 0;i<Model.Count();i++)
{
<tr>
<td>#Model[i].Number</td>
<td><input type="text" readonly="readonly" asp-for="#Model[i].GoodsName" class="form-control" /></td>
<td><input type="text" readonly="readonly" asp-for="#Model[i].BrandName" class="form-control" /></td>
<td><input type="text" readonly="readonly" asp-for="#Model[i].Quantity" class="form-control" /></td>
<td><input type="text" readonly="readonly" asp-for="#Model[i].ScaleName" class="form-control" /></td>
<td>
<select class="form-control" asp-for="#Model[i].Status[0].Id" asp-items="#(new SelectList(Model[i].Status,"Id","Name"))">
</select>
</td>
</tr>
}
<tr>
<td colspan="6">
<textarea class="form-control" rows="3" readonly="readonly" cols="5">#Model.Select(s => s.Description).First()</textarea>
</td>
</tr>
</tbody>
</table>
<a class="btn btn-primary">Back</a>
<input type="submit" value="Submit" class="btn btn-success" style="width:auto">
</form>

Updating DB-How do I fix "ViewModel is not part of the model for the current context"

I created a view model and associated controller and view, but it looks like there's a piece missing in both my head and my code. When I submit the page, with or without DB changes, I get the following. "The entity type ReportReviewViewModel is not part of the model for the current context."
Models (not all fields)
public partial class RFDS
{
public System.Guid rfds_processing_id { get; set; }
public string location_name { get; set; }
public string address { get; set; }
}
public partial class Sub_Clients_Xref
{
public int id { get; set; }
public string sub_client { get; set; }
public string sub_client_attn { get; set; }
public string sub_client_address { get; set; }
}
public partial class Engineering_License_Xref
{
public int id { get; set; }
public string state_name { get; set; }
public string license_number { get; set; }
}
ViewModel
public class ReportReviewViewModel
{
public RFDS rfds { get; set; }
public Sub_Clients_Xref subclient { get; set; }
public Engineering_License_Xref engineer { get; set; }
public States_Xref state { get; set; }
}
Controller
public ActionResult Review(Guid? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
RFDS rfds_db = db.RFDS.Find(id);
if (rfds_db == null)
{
return HttpNotFound();
}
List<RFDS> rfds1 = db.RFDS.ToList();
List<Sub_Clients_Xref> subclients = db.Sub_Clients_Xref.ToList();
List<Engineering_License_Xref> engineers = db.Engineering_License_Xref.ToList();
List<States_Xref> states = db.States_Xref.ToList();
var viewModel = (from r in rfds1
join s in subclients on r.sub_client equals s.sub_client
join st in states on r.state equals st.state
join e in engineers on r.engineer_name equals e.engineer_name
where r.rfds_processing_id == id && r.sub_client == s.sub_client &&
(r.engineer_name == e.engineer_name && e.state_name == st.state_name)
select new ReportReviewViewModel
{
rfds = r,
subclient = s,
engineer = e
});
return View(viewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Review(ReportReviewViewModel viewModel)
{
if (ModelState.IsValid)
{
db.Entry(viewModel).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Review");
}
return View(viewModel);
}
UPDATED CONTROLLER
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Review(ReportReviewViewModel viewModel)
{
if (ModelState.IsValid)
{
db.Entry(viewModel.rfds).State = EntityState.Modified;
db.Entry(viewModel.engineer).State = EntityState.Modified;
db.Entry(viewModel.subclient).State = EntityState.Modified;
db.Entry(viewModel.state).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Review");
}
VIEW example
#model IEnumerable<TeamWeb.ViewModels.ReportReviewViewModel>
#{
ViewBag.Title = "Review";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="container2">
<div class="nav col-md-3">
#Html.ActionLink("BACK TO LIST", "Index")
</div>
<hr />
<table class="rev-table-main" border="0">
<!----------------------------- EDIT DATA ----------------------------->
#using (Html.BeginForm("Review", "RFDS", FormMethod.Post))
{
#Html.AntiForgeryToken()
foreach (var item in Model)
{
#Html.HiddenFor(model => item.rfds.rfds_processing_id)
<tr>
<td class="rev-values-td">
<table class="rev-table-values" border="0">
#*<tr class="rev-tr">
<td> </td>
</tr>*#
<tr class="rev-tr">
<td style="font-size:medium;font-weight:600;text-align:center;" colspan="2">
Edit Report Information
</td>
</tr>
<tr>
<td style="text-align:right;" width="40%">
Site Name:
</td>
<td width="60%">
#Html.EditorFor(model => item.rfds.location_name)
</td>
</tr>
<tr>
<td align="right">
FA Location Code:
</td>
<td>
#Html.EditorFor(model => item.rfds.fa_location_code)
</td>
</tr>
<tr>
<td align="right">
Site Address:
</td>
<td>
#Html.EditorFor(model => item.rfds.address)
</td>
</tr>
<tr>
<td align="right">
Site City:
</td>
<td>
#Html.EditorFor(model => item.rfds.city)
</td>
</tr>
<tr>
<td align="right">
Site State:
</td>
<td>
#Html.EditorFor(model => item.rfds.state)
</td>
</tr>
<tr>
<td align="right">
Site Zip Code:
</td>
<td>
#Html.EditorFor(model => item.rfds.zip_code)
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td align="right">
Sub Client:
</td>
<td>
#Html.EditorFor(model => item.subclient.sub_client)
</td>
</tr>
<tr>
<td align="right">
Sub Client Attn:
</td>
<td>
#Html.EditorFor(model => item.subclient.sub_client_attn)
</td>
</tr>
<tr>
<td align="right">
Sub Client Address:
</td>
<td>
#Html.EditorFor(model => item.subclient.sub_client_address)
</td>
</tr>
<tr>
<td align="right">
Sub Client City:
</td>
<td>
#Html.EditorFor(model => item.subclient.sub_client_city)
</td>
</tr>
<tr>
<td align="right">
Sub Client State:
</td>
<td>
#Html.EditorFor(model => item.subclient.sub_client_state)
</td>
</tr>
<tr>
<td align="right">
Sub Client Zip Code:
</td>
<td>
#Html.EditorFor(model => item.subclient.sub_client_zip)
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td align="right">
Capability %:
</td>
<td>
#Html.EditorFor(model => item.rfds.capability)
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td align="right">
Report Date:
</td>
<td>
#Html.EditorFor(model => item.rfds.submit_date)
</td>
</tr>
<tr>
<td align="right">
Job Number:
</td>
<td>
#Html.EditorFor(model => item.rfds.job_number)
</td>
</tr>
<tr>
<td align="right">
Revision:
</td>
<td>
#Html.EditorFor(model => item.rfds.revision)
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Save" />
</td>
</tr>
</table>
</td>
</tr>
}
}
</table>
</div>

Value of HttpFileCollectionBase Isnull on Postback

I'm curious instead of using HTTPFileWrapper, i created a model with HttpFileCollectionBase type of property.
public class UserLoginModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "password")]
public string Password { get; set; }
public HttpFileCollectionBase Resume { get; set; }
}
My View
#using (Html.BeginForm("", "User", FormMethod.Post, new { enctype = "multipart/form-data", #class = "form-horizontal" }))
{
// #Html.AntiForgeryToken()
<table>
<tr>
<td>
#Html.LabelFor(x => x.UserName)
</td>
<td>
#Html.EditorFor(x => x.UserName)
</td>
</tr>
<tr>
<td>
#Html.LabelFor(x => x.Password)
</td>
<td>
#Html.EditorFor(x => x.Password)
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="login" onclick="return Upload();">
</td>
</tr>
<tr>
<td colspan="2">
#Html.TextBoxFor(x => x.Resume, new { type = "file" })
</td>
</tr>
<tr>
<td colspan="2">
#Html.ValidationSummary(true)
</td>
</tr>
</table>
}
And my Controller for postback
[HttpPost]
public ActionResult Index(UserLoginModel obj)
but when I checked the value of model.Resume on the controller the value was null. what could be the reason for that? thanks
You using the wrong class. Your property needs to be HttpPostedFileBase
public class UserLoginModel
{
....
public HttpPostedFileBase Resume { get; set; }
}