'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' 'object' does not contain a definition for 'intEmployeeId' - linq

I am getting an exception when i trying to display the values of viewbag. I have joined two tables and stored that data in viewbag using Linq.
This is my controller action method
public ActionResult HRSeparationDetails()
{
//List<trnEsSeparationDetail> separationlist = (from list in db.trnEsSeparationDetails
// select list).ToList();
ViewBag.SeparationList = (from list in db.trnEsSeparationDetails
join R in db.mstEsEmpReasonForSeparations
on list.ReasonForSeperationId equals R.intSeperationId
select new
{
intEmployeeId = list.intEmpId,
ReasonForSeparation = R.txtReasonForSeperation,
ResiganationDate = list.dtResignationDate,
RelievingDate = list.dtRequestedRelievingDate,
SeparationRemarks = list.txtRemark
}).ToList();
return View();
}
This is my viewpage
#model IEnumerable<EMPApp.Models.trnEsSeparationDetail>
#{
ViewBag.Title = "HRSeparationDetails";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>HRSeparationDetails</h2>
<div>
#if (ViewBag.SeparationList == null)
{
<div style="color:green"><h3>#ViewBag.SeparationerrorMessage</h3></div>
}
else
{
<table class="grid" cellpadding="0" cellspacing="0">
<tr>
<th>
<b>Employee Id</b>
</th>
#*<th>
<b>ReasonForSeparationId</b>
</th>*#
<th>
<b>Reason For Separation</b>
</th>
<th>
<b>Resignation Date</b>
</th>
<th>
<b>Relieving Date</b>
</th>
<th>
<b>Separation Remarks</b>
</th>
</tr>
#foreach (var item in ViewBag.SeparationList)
{
<tr>
<td>
#item.intEmployeeId
#*#{
Session["SeparationEmpId"] = item.intEmpId;
}
#Html.ActionLink(Session["SeparationEmpId"].ToString(), "EmployeeDashboard", "HRAdmin", new { id = #item.intEmpId }, null)*#
</td>
#*<td>
#item.ReasonForSeperationId
</td>*#
<td>
#item.ReasonForSeparation
</td>
<td>
#item.ResiganationDate
</td>
<td>
#item.RelievingDate
</td>
<td>
#item.SeparationRemarks
</td>
</tr>
}
</table>
}
Any help will be appreciated. Thanks..

ViewBag is a dynamic object and your setting the property to a collection of anonymous objects. In you foreach loop
#foreach (var item in ViewBag.SeparationList)
item is typeof object and object does not have a property intEmployeeId (or any of the other properties your define in the anonymous object). One way to handle this is to create a view model with the properties you want to display
public class SeparationDetailVM
{
public int intEmployeeId { get; set; }
public DateTime ResiganationDate { get; set; }
....
}
and in the controller
var details = (from list in ....
select new SeparationDetailVM
{
intEmployeeId = list.intEmpId,
ResiganationDate = list.dtResignationDate,
....
}).AsEnumerable();
return View(details); // no need for ViewBag
and in the view
#model IEnumerable<YourAssembly.SeparationDetailVM>
....
#foreach (var item in Model)
{
....
<td>#item.intEmployeeId</td>
<td>#item.ResiganationDate</td>
....
}

Related

DropdownlistFor in a loop, null model on submit

I am having an issue with the values selected in multiple dropdownlistfor(s) being available in the controller when the form is submitted. The model is always blank. I know there are issues with mvc having dropdowns in loops but I thought I have solved for this. Let me know what you think.
View
#model DataDictionaryConversion.Models.FinalResults
#{ using (Html.BeginForm("SaveMapping", "Home", FormMethod.Post, null))
{
#Html.AntiForgeryToken()
<table class="table table-striped">
<thead>
<tr>
<th>Converted to Name</th>
<th>Your Project Name</th>
<th><input type="button"
onclick="checkAll()"/></th>
</tr>
</thead>
<tbody>
#{for (int x = 0; x < Model.DDObjects.Count(); x++)
{
var isSelection = false;
<tr>
<td class="filterable-cell">#Model.DDObjects[x].ObjectName</td>
<td class="filterable-cell">
#Html.DropDownList(Model.DDObjects[x].ObjectName, new
SelectList(Model.ProjectObjects, "ObjectName", "ObjectName"),
htmlAttributes: new { #id = "ddlObject", #class = "js-example-basic-single" })</td>
</td>
<td>
<input type="checkbox" id="NoValue-
#Model.DDObjects[x].ObjectName" name="NoValue-
#Model.DDObjects[x].ObjectName" onclick="byPassObject(this)" /> Object
Not
Used
</td>
</tr>
}
}
</tbody>
<tfoot>
<tr>
<td style="text-align:right; height:20px"><input
type="submit" class="btn btn-warning" value="Generate Conversion Mapping"
/></td>
</tr>
</tfoot>
</table>
}
}
Controller
[HttpPost]
public ActionResult SaveMapping([FromServices]ApplicationDbContext context, FinalResults model)
{
return View("Mapping");
}
Model
public class FinalResults
{
public IList<FinalObjectModel> ProjectObjects { get; set; }
public IList<Conversion_CSD_ObjectNameLearningModel> DDObjects {
get; set; }
FinalResults model is null
You're using Html.DropDownList. The first param there is a string, which should correspond with the name you're binding to. However, you're passing the value of Model.DDObjects[x].ObjectName, not literally something like "DDOjbects[0].ObjectName".
Instead, you should be using Html.DropDownListFor like so:
#Html.DropDownListFor(m => m.DDObjects[x].ObjectName, ...)
Then, the select list will be bound correctly.

Error on page: The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies

I get this error on my Details view page:
The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.cpd_certificates_65AF30842281867E2F4F6A1026590271109C12A85C8175394F14EF6DE429CBC7', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[cpd.Models.cpd_certificates]'.
This is my controller :
public class Default8Controller : Controller
{
//
// GET: /Default8/
private cpdDbContext db = new cpdDbContext();
public ActionResult Index()
{
return View(db.CPD.ToList());
}
//
// GET: /Default8/Details/5
public ActionResult Details(int id)
{
cpd_certificates cpd_ = db.Cert.Find(id);
return View(cpd_);
}
Below is my Details view:
#model IEnumerable<cpd.Models.cpd_certificates>
#{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<table>
<tr>
<th>
certificateNo
</th>
<th>
Mark
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.CertificateNo)
</td>
<td>
#Html.DisplayFor(modelItem => item.Mark)
</td>
</tr>
}
</table>
Below is the cpd_certificates model:
public class cpd_certificates
{
[Key]
public int CertificateNo { get; set; }
public int QuizNo { get; set; }
public DateTime? DateReceived { get; set; }
public DateTime? DatePaid { get; set; }
public string Mark { get; set; }
public int? AccreditationNo { get; set; }
public int? ID { get; set; }
public virtual cpd_recipients Recipients { get; set; }
}
I have two models and I can view the list on my Index. I intended that when I click the details link it takes me to details of certificates earned and exams which are in another model/table. So in short two models which have a PK ID for CPD and FK ID on Cert. Index page show just personal information and this page is fine.
When Details is clicked it then shows the other model/table data that is the many certificates of that person.
You should pass the model into the view as the List of IEnumerable entries instead of passing it as a single entry.
In your Controller you want to show one entry details, but in the view, want to show details of all entries.First you should decide what to show.
For showing one entry details, use this:
controller:
public ActionResult Details(int id)
{
cpd_certificates cpd_ = db.Cert.Find(id);
return View(cpd_);
}
view:
#model cpd.Models.cpd_certificates
#{
ViewBag.Title = "Details";}
<h2>Details</h2>
<table>
<tr>
<th>
certificateNo
</th>
<th>
Mark
</th>
<th></th>
</tr>
<tr>
<td>
#Html.DisplayFor(model => model.CertificateNo)
</td>
<td>
#Html.DisplayFor(model => model.Mark)
</td>
</tr>
}
</table>
And when you want to list all of entries, use this:
controller:
public ActionResult List()
{
cpd_certificates cpd_ = db.Cert.ToList();
return View(cpd_);
}
view:
#model IEnumerable<cpd.Models.cpd_certificates>
#{
ViewBag.Title = "List";
}
<h2>List</h2>
<table>
<tr>
<th>
certificateNo
</th>
<th>
Mark
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.CertificateNo)
</td>
<td>
#Html.DisplayFor(modelItem => item.Mark)
</td>
</tr>
}
</table>

How to pass IEnumerable object or data from view to controller?

I have created a strongly type view. I want to pass IEnumerable Object or data from View to Controller. Following are my Model, Controller ,view
My Model:
public class UserDetailsClass
{
public static FeedbackDatabaseDataContext context = new FeedbackDatabaseDataContext();
public class Tablefields
{
[Key]
public int ID { get; set; }
[Required(ErrorMessage = "Email is required")]
public string EmailID { get; set; }
[Required(ErrorMessage="Password is required")]
public string Password { get; set; }
[Required(ErrorMessage = "First Name is required")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last Name is required")]
public string LastName { get; set; }
}
public static List<UserDetails> getalluser()
{
var lst = (from r in context.UserDetails select r);
return lst.ToList();
}
}
My Controller
public ActionResult Test()
{
IList<UserDetailsClass.Tablefields> viewmodel = new List<UserDetailsClass.Tablefields>();
var q = UserDetailsClass.getalluser().ToList();
foreach (SQLOperation.Models.UserDetails item in q)
{
UserDetailsClass.Tablefields viewItem = new UserDetailsClass.Tablefields();
viewItem.EmailID = item.Email;
viewItem.FirstName = item.FirstName;
viewItem.LastName = item.LastName;
viewItem.Password = item.Password;
viewmodel.Add(viewItem);
}
return View(viewmodel);
}
[HttpPost]
public ActionResult Test(IEnumerable<UserDetailsClass.Tablefields> items)
{
return View();
}
My View:
#model IEnumerable<SQLOperation.Models.UserDetailsClass.Tablefields>
#{
ViewBag.Title = "Test";
}
<h2>Test</h2>
#using (Html.BeginForm())
{
<table>
<tr>
<th>
EmailID
</th>
<th>
Password
</th>
<th>
FirstName
</th>
<th>
LastName
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.EmailID)
</td>
<td>
#Html.DisplayFor(modelItem => item.Password)
</td>
<td>
#Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
#Html.DisplayFor(modelItem => item.LastName)
</td>
</tr>
}
</table>
<input type="submit" value="submit" />
}
I didn't get any value in items.When I set breakpoint on it it show NULL in items
I am totally Confused about how to pass values to controller.
Thank you,
Amol
Instead of #Html.DisplayFor use #Html.EditorFor, that way databinding will happen in your form.
Also since you are using collection you need to iterate your fields (simplified example):
#for (int row = 0; row < Model.Count; row++)
{
#Html.EditorFor(x => x[row].FirstName )
}
#for (int i = 0; i < Model.length; i ++)
{
<tr>
<td>
#Html.DisplayFor(model => Model[i].EmailID)
</td>
<td>
#Html.DisplayFor(model => Model[i].Password)
</td>
<td>
#Html.DisplayFor(model => Model[i].FirstName)
</td>
<td>
#Html.DisplayFor(model => Model[i].LastName)
</td>
</tr>
}
</table>
<input type="submit" value="submit" />
}

Show images in table from database in Asp.net-mvc3

I have a table containing user information with image field and storing image in binary data.
I want to show this images on my view page in a table with user information.
my code is..
Controller
public ActionResult About()
{
var data = db.dbtable.ToList();
return View(data);
}
Get Image method // reg is object of dbtable
public FileContentResult GetImage(int id)
{
reg = db.dbtable.FirstOrDefault(i => i.RegNo == id);
if (reg != null)
{
return File(reg.Photo, "image/jpeg");
}
else
{
return null;
}
}
on view page..
#model IEnumerable<MvcMySchool.dbtable>
<table width="50">
<tr>
<th>
Registration No.
</th>
<th>
First Name
</th>
<th>
Last Name
</th>
<th>
City
</th>
<th>
Photo
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.RegNo)
</td>
<td>
#Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
#Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
#Html.DisplayFor(modelItem => item.City)
</td>
<td>
#*HttpContext.Current.Response.Write("<img width="75" height="75" src=#(Url.Action("GetImage", "Home", new { id = item.RegNo })) />")*#
<img width="75" height="75" src=#(Url.Action("GetImage", "Home", new { id = item.RegNo })) />
</td>
</tr>
}
</table>
It only shows the image in the first row and nothing after that.
Thanks..
May make more sense just to keep the path the image.
If you filesteam,
public FileStreamResult GetImage(int id)
{
reg = db.dbtable.FirstOrDefault(i => i.RegNo == id);
if (reg != null)
{
MemoryStream ms = new MemoryStream(reg.Photo);
return File(ms, "image/png");
}
else
{
return null;
}
}
more logical way to keep, ony way
public FileResult GetImage(int id)
{
reg = db.dbtable.FirstOrDefault(i => i.RegNo == id);
if (reg != null)
{
FileInfo fi = new FileInfo(Server.MapPath(reg.Photo));
return File(fi.OpenRead, "image/png");
}
}
for both
<tr>
<td>
<img src="/Home/GetImage/#item.RegNo" alt="" />
</td>
</tr>

MVC3 The model item passed into the dictionary is of type '_320.Models.MyViewModel', but

I am passing a viewmodel into a view containing a foreach loop. This viewmodel contains two lists. These lists are filled using Linq.
With the following code, I get the following error: The model item passed into the dictionary is of type '_320.Models.MyViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[_320.Models.MyViewModel]'.
I want to reach properties contained in the two lists in the ViewModel in my view's foreach loop.
UPDATE
I have been able to display the view by modifying the end of my controller code to:
List<MyViewModel> MyList = new List<MyViewModel>();
MyList.Add(_myViewModel);
return View(MyList);
However, it is an empty view with nothing but headers. When I attempt to change my fields in my view from something like #Html.DisplayFor(modelItem => item.rx.Rx_ID) to #Html.DisplayFor (modelItem => item.Rxes.rx.Rx_ID) it is labeled red. Is it impossible to access a property located that far down in a list?
END OF UPDATE
Model
public class MyViewModel
{
public Patient patient { get; set; }
public Rx rx { get; set; }
public Fill fill { get; set; }
public List<Rx> Rxes { get; set; }
public List<Fill> Fills { get; set; }
Controller
public ActionResult AllFilled()
{
MyViewModel _myViewModel = new MyViewModel();
List<Fill> FillList = db.Fills.Where(p => p.Status == "Filled").ToList();
List<Rx> RxList = new List<Rx>();
Rx myRx = new Rx();
foreach (var item in FillList)
{
myRx = db.Rxes.Single(p => p.Rx_ID == item.Rx_ID);
RxList.Add(myRx);
}
_myViewModel.Fills = FillList;
_myViewModel.Rxes = RxList;
return View(_myViewModel);
}
}
View
#model IEnumerable<_320.Models.MyViewModel>
#{
ViewBag.Title = "AllFilled";
}
<h2>AllFilled</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
NDC
</th>
<th>
Rx_ID
</th>
<th>
Fill_ID
</th>
<th>
Status
</th>
<th>
Filled Date
</th>
<th>
Filled By
</th>
<th></th>
</tr>
{
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.rx.NDC)
</td>
<td>
#Html.DisplayFor(modelItem => item.rx.Rx_ID)
</td>
<td>
#Html.DisplayFor(modelItem =>item.fill.Fill_ID)
</td>
<td>
#Html.DisplayFor(modelItem => item.fill.Status)
</td>
<td>
#Html.DisplayFor(modelItem => item.fill.Filled_Date)
</td>
<td>
#Html.DisplayFor(modelItem => item.fill.Filled_By)
</td>
<td>
#Html.ActionLink("Details", "Details", new { id=item.rx.Rx_ID })
</td>
</tr>
}
</table>
Your view is expecting a model of type IEnumerable<_320.Models.MyViewModel> due to the first line in the view.
#model IEnumerable<_320.Models.MyViewModel>
If you want to use a model object of type MyViewModel (as coded in your controller), change this line to
#model _320.Models.MyViewModel

Resources