How to deal with inherited entity in EF and MVC app? - asp.net-mvc-3

I am developing MVC application and using razor syntax.
I have used model first method.
I have two entities, Customer and Lead. Lead is inherited from Customer.
When IsActive property is true then Customer treated as a Lead, otherwise it will be a customer.
Please check edmx file image.
When I genrated DB from model , I get the two tables Customer Table and Customer_Lead
table.
This is Customer table.
and This is Customer_Lead Table.
Now the problem is when I run index view of lead entity , Its give the error.
The model item passed into the dictionary is of type
PagedList.PagedList1[CRMEntities.Customer], but this dictionary
requires a model item of type
PagedList.IPagedList1[CRMEntities.Lead].
I am getting confused abt how to write a view for the entity which is inherited from other entity and how to access the data from these two tables while working with inherited entity.
Index View code is as below...
#model PagedList.IPagedList<CRMEntities.Lead>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
Status
</th>
<th>
IsQualified
</th>
<th>
Name
</th>
<th>
Address
</th>
<th>
OfficePhone1
</th>
<th>
Website
</th>
<th>
Email2
</th>
<th>
Email1
</th>
<th>
FaxNo
</th>
<th>
Remark
</th>
<th>
Rating
</th>
<th>
BusinessType
</th>
<th>
IsActive
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Status)
</td>
<td>
#Html.DisplayFor(modelItem => item.IsQualified)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Address)
</td>
<td>
#Html.DisplayFor(modelItem => item.OfficePhone1)
</td>
<td>
#Html.DisplayFor(modelItem => item.Website)
</td>
<td>
#Html.DisplayFor(modelItem => item.Email2)
</td>
<td>
#Html.DisplayFor(modelItem => item.Email1)
</td>
<td>
#Html.DisplayFor(modelItem => item.FaxNo)
</td>
<td>
#Html.DisplayFor(modelItem => item.Remark)
</td>
<td>
#Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
#Html.DisplayFor(modelItem => item.BusinessType)
</td>
<td>
#Html.DisplayFor(modelItem => item.IsActive)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
#Html.ActionLink("Details", "Details", new { id=item.Id }) |
#Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
I have following code in index...
public ViewResult Index()
{
var LeadList = (from c in db.Customers
where (c.IsActive == true)
orderby (c.Id)
select c);
//What should I return ?
}

Your view looks fine, but you're trying to pass a list of Customers to it. Try getting a list of Lead customers from context.
public ActionResult Index()
{
...
var leadList = db.Customers.OfType<CRMEntities.Lead>().Where(c => c.IsActive).OrderBy(c => c.Id);
return View(leadList);
}

Related

PartialView is not working (whole page refresh instead)

Hello I am new to MVC 4. I am having a problem with partialView.. I goole it and searched alot and studied different tutorials but couldn't find any solution that can address my problem.I created a PagedList to display records at Index.cshtml page. Then I created PartialIndex.cshtml page to display records in Partial View.
Here is problem: When I click on any page number or navigate.. whole page refreshes and post back... partial view is not working.Don't Know where I am doing wrong.
I want to show table inside the DIV in PartialIndex.cshtml
PartialIndex.cshtml:
<div id="targetContainer"> //I want to show this DIV in partial view.
<table>
<tr>
<th>
#Html.ActionLink("ID", "Index", new { sortOrder = ViewBag.customerID, currentFilter=ViewBag.CurrentFilter })
</th>
<th>
</th>
<th>
#Html.DisplayName("First Name")
</th>
<th></th>
<th>
#Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.lName, currentFilter=ViewBag.CurrentFilter })
</th>
<th></th>
<th>
#Html.DisplayName("Contact Num")
</th>
<th>
#Html.DisplayName("Address")
</th>
<th>
#Html.DisplayName("Email")
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.customerId)
</td>
<td>
</td>
<td>
#Html.DisplayFor(modelItem => item.fName)
</td>
<td>
</td>
<td>
#Html.DisplayFor(modelItem => item.lName)
</td>
<td>
</td>
<td>
#Html.DisplayFor(modelItem => item.contactNum)
</td>
<td>
</td>
<td>
#Html.DisplayFor(modelItem => item.address)
</td>
<td>
</td>
<td>
#Html.DisplayFor(modelItem => item.email)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.ID })
#Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
</div>
<br />
Page #(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of #Model.PageCount
#Html.PagedListPager(Model, page => Url.Action("Index",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(
new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "GET", UpdateTargetId = "targetContainer" }))
Index.cshtml:
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#Html.Partial("PartialIndex")
You should better use
Html.RenderPartial("~/Views/Shared/_Yourpartial.cshtml")
Html.RenderPartial("~/Views/Shared/PartialIndex.cshtml")

How to display Company in header in asp.net mvc 3

I have this view :
model IEnumerable<MvcApplication4.Models.Order>
#{
ViewBag.Title = "Index";
}
<h2>
display CompanyName
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
Customer
</th>
<th>
EmployeeID
</th>
<th>
OrderDate
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Customer.CompanyName)
</td>
<td>
#Html.DisplayFor(modelItem => item.EmployeeID)
</td>
<td>
#Html.DisplayFor(modelItem => item.OrderDate)
</td>
</tr>
}
</table>
I want to display the CompanyName where I marked in bold. At the moment its being displayed in the foreach loop. However I want that one field which is same for this view to pull the CompanyName and display it in between my headings.
How about putting
#{
if (Model.Any()) {
Model.First().Customer.CompanyName
}
}
where your bold text is?

How to display IEnumerable item in asp.net mvc 3

I have this view :
#model IEnumerable<sn.Models.Order>
<h2>
**Name of Recipients**
</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
Last Name
</th>
<th>
Mark
</th>
<th>
Quiz Number
</th>
<th>
Pass
</th>
<th>
Paid
</th>
<th>
Mark Date
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Customer.LastName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Mark)
</td>
<td>
#Html.DisplayFor(modelItem => item.QuizNo)
</td>
<td>
#Html.DisplayFor(modelItem => item.Pass)
</td>
<td>
#Html.DisplayFor(modelItem => item.Paid)
</td>
<td>
#Html.DisplayFor(modelItem => item.MarkDate)
</td>
</tr>
}
</table>
below is an image showing my view:
I want to display the LastName in place of Name of Recipients . The item LastName does not change for a particular customer. This view is showing the details of that user and this is part of the code I used to get the results in my controller:
var certificateDetails = db.orders.Where(p => p.ID == id);
return View( certificateDetails.ToPagedList(pageNumber, pageSize));
<h2>
#(Model.Any() ? Model.First().Customer.LastName : "")
</h2>

MVC 3 Ajax.BeginForm submit issue

im trying to submit a form by pressing a button that eventually will refresh the table
this is the form :
#{
AjaxOptions ajaxOpts = new AjaxOptions
{
Confirm = "start ???",
UpdateTargetId = "MainTable",
InsertionMode = InsertionMode.Replace,
Url = Url.Action("Refresh","MainScreen"),
LoadingElementId = "loading",
LoadingElementDuration = 2000,
};}
#using (Ajax.BeginForm(ajaxOpts)){
<div id="loading" style="display: none; color: Red; font-weight: bold">
<p>
Loading Data...</p>
</div>
<div id="header">
<input type="button" value="Submit Form" />
</div>
<table id="MainTable">
<tr>
<th>
ServiceId
</th>
<th>
ServiceInstanceId
</th>
<th>
MessageRole
</th>
<th>
Datetime
</th>
<th>
Message
</th>
<th>
Status
</th>
<th>
ESBErrorCode
</th>
<th>
ESBTecnicalErrorCode
</th>
<th>
ErrorDescription
</th>
<th>
PortName
</th>
<th>
MachineName
</th>
<th>
ConsumerId
</th>
<th>
ExternalId
</th>
<th>
ConsumerMachineName
</th>
<th>
ServiceBehavior
</th>
<th>
RouterConsumerId
</th>
<th>
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.ServiceId)
</td>
<td>
#Html.DisplayFor(modelItem => item.ServiceInstanceId)
</td>
<td>
#Html.DisplayFor(modelItem => item.MessageRole)
</td>
<td>
#Html.DisplayFor(modelItem => item.Datetime)
</td>
<td>
\
#Html.DisplayFor(modelItem => item.Message.Context)
</td>
<td>
#Html.DisplayFor(modelItem => item.Status)
</td>
<td>
#Html.DisplayFor(modelItem => item.ESBErrorCode)
</td>
<td>
#Html.DisplayFor(modelItem => item.ESBTecnicalErrorCode)
</td>
<td>
#Html.DisplayFor(modelItem => item.ErrorDescription)
</td>
<td>
#Html.DisplayFor(modelItem => item.PortName)
</td>
<td>
#Html.DisplayFor(modelItem => item.MachineName)
</td>
<td>
#Html.DisplayFor(modelItem => item.ConsumerId)
</td>
<td>
#Html.DisplayFor(modelItem => item.ExternalId)
</td>
<td>
#Html.DisplayFor(modelItem => item.ConsumerMachineName)
</td>
<td>
#Html.DisplayFor(modelItem => item.ServiceBehavior)
</td>
<td>
#Html.DisplayFor(modelItem => item.RouterConsumerId)
</td>
</tr>
}
</table>
}
this is the Controler :
public ViewResult Refresh()
{
var tracks = db.Tracks.Include(t => t.Message);
return View(tracks.ToList());
}
for some reason when i submit the button nothing happen
i already added
<script src="#Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
thanks
miki
The unobtrusive js helper listens on the submit event with
$("form[data-ajax=true]").live("submit"...
but your "submit" button is "just a button" not a submit button:
<input type="button" value="Submit Form" />
Change it to type="submit"and it should work:
<input type="submit" value="Submit Form" />

How to print 2 columns of pictures from a list in asp.net mvc3

I have this code and I am a bit in doubt how to make it print 2 rows instead of just 1 (Later I want it to be dynamic depending on screensize if possible, but for now it should just be 2 columns)
http://pastebin.com/H7CpBWWN
#model IEnumerable<FirstWeb.Models.Picture>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
Title
</th>
<th>
Path
</th>
<th>
ConcertYear
</th>
<th>
Title
</th>
<th>
Path
</th>
<th>
ConcertYear
</th>
</tr>
#bool even = false;
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
<a href=#Html.DisplayFor(modelItem => item.Path)><img src=#Html.DisplayFor(modelItem => item.Path) width="250px" /> </a>
</td>
<td>
#Html.DisplayFor(modelItem => item.ConcertYear)
</td>
</tr>
}
</table>
So I want the boolean to be the switch between left and right column
I don't know how you can do it with your boolean flag for left or right, but if you are just after two columns, you could do something like this (apologies for typos/syntax errors, but you get the idea).
#for (int i = 0; i < Model.ContactMethods.Count; i += 2)
{
<tr>
<td>
#Html.DisplayFor(modelItem => Model[i].Title)
</td>
<td>
<a href=#Html.DisplayFor(modelItem => Model[i].Path)>
<img src=#Html.DisplayFor(modelItem => Model[i].Path) width="250px" />
</a>
</td>
<td>
#Html.DisplayFor(modelItem => Model[i].ConcertYear)
</td>
#if (Model.ContactMethods.Count > i + 1)
{
<td>
#Html.DisplayFor(modelItem => Model[i + 1].Title)
</td>
<td>
<a href=#Html.DisplayFor(modelItem => Model[i + 1].Path)>
<img src=#Html.DisplayFor(modelItem => Model[i + 1].Path) width="250px" />
</a>
</td>
<td>
#Html.DisplayFor(modelItem => Model[i + 1].ConcertYear)
</td>
}
else
{
<td colspan="3"></td>
}
</tr>
}
As far as dynamic columns, you'll need to look at jquery for that

Resources