redirect to a action that expects value in other controller - asp.net-mvc

I have a controller StepOfIdea,and this controller has a action like this :
StepOfIdeaRepository objStepOfIdearepository=new StepOfIdeaRepository();
public ActionResult Index(int ideaId)
{
return View(objStepOfIdearepository.FindBy(i=>i.IdeaId==ideaId));
}
So i have another controller named idea and this controller has a view named index
#model IEnumerable<DomainClass.Idea>
#{
ViewBag.Title = "Index";
}
<h2>لیست</h2>
#if (User.IsInRole("User"))
{
<p>
#Html.ActionLink("ایده جدید", "Create", new {step = 1})
</p>
}
<table>
<tr>
#if (User.IsInRole("Admin"))
{
<th>
#Html.DisplayNameFor(model => model.User.Name)
</th>
}
<th>
#Html.DisplayNameFor(model => model.IdeaPersion)
</th>
<th>
#Html.DisplayNameFor(model => model.IdeaEnglish)
</th>
<th>
#Html.DisplayNameFor(model => model.IdeaResult)
</th>
<th>
#Html.DisplayNameFor(model => model.Date)
</th>
<th></th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
#if (User.IsInRole("Admin"))
{
<td>
#Html.DisplayFor(modelItem => item.User.Name) #Html.DisplayFor(modelItem => item.User.Name)
</td>
}
<td>
#Html.DisplayFor(modelItem => item.IdeaPersion)
</td>
<td>
#Html.DisplayFor(modelItem => item.IdeaEnglish)
</td>
<td>
#Html.DisplayFor(modelItem => item.IdeaResult)
</td>
<td>
#Html.DisplayFor(modelItem => item.Date)
</td>
<td>
#Html.RenderAction("ویرایش","index", stepofidea, new { id=item.Id }) |
</td>
<td>
#Html.ActionLink("ویرایش", "Edit", new { id=item.Id }) |
#Html.ActionLink("نمایش", "Edit", new { id=item.Id }) |
#Html.ActionLink("حذف", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
In this line
#Html.RenderAction("ویرایش","index", ????, new { id=item.Id }) |
I want to redirect to index action of stepOfIdea controller and pass a value .But the above line doesn't work .

I think you confused some terms in translation to English, and what you are actually looking for is to create an Link that also passes a variable.
You can do this very simply, by:
#Html.ActionLink("ویرایش", "Index", "StepOfIdea", new { id = item.Id }, null)
This will create the HTML:
ویرایش

Related

Object reference not set to an instance of an object. First login. solved when I rerun the application?

When I create a user and he's logged in for the first time I get this error when I try to display(View details) the requests that he created so far(0 request created). Once I rerun the application again with the same user the view works and I get no error. Here's the line that's generates the error:
#foreach (var item in Model)`
Here's the view that generates the error:
#model IEnumerable<Mutuelle.Domain.Entities.Demande>
#{
ViewBag.Title = "DemandesAdherent";
}
#{
Layout = "~/Views/Shared/_LayoutAdherent.cshtml";
}
<h2>DemandesAdherent</h2>
<p>
#Html.ActionLink("Creér nouvelle", "AddDemande")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Date)
</th>
<th>
#Html.DisplayNameFor(model => model.nomBeneficiaire)
</th>
<th>
#Html.DisplayNameFor(model => model.Nomrubrique)
</th>
<th>
#Html.DisplayNameFor(model => model.etat)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Date)
</td>
<td>
#Html.DisplayFor(modelItem => item.nomBeneficiaire)
</td>
<td>
#Html.DisplayFor(modelItem => item.Nomrubrique)
</td>
<td>
#Html.DisplayFor(modelItem => item.etat)
</td>
<td>
#Html.ActionLink("Details", "DetailsDemande", new { id=item.DemandeId }) #*|
#Html.ActionLink("Delete", "DeleteDemande", new { id=item.DemandeId })*#
</td>
</tr>
}
</table>

how to make model into IEnumerable for the view [duplicate]

This question already has answers here:
The model item passed into the dictionary is of type .. but this dictionary requires a model item of type
(7 answers)
Closed 5 years ago.
This is my ActionResult
public ActionResult mytry()
{
Empoyee ee = new Empoyee();
ee.Address = "address1";
ee.Email = "email1";
ee.FirstName = "fname";
ee.Id = 23;
ee.LastName = "lname1";
ee.MiddleName = "";
ee.Salary = 20000;
db.Empoyees.Add(ee);
db.SaveChanges();
return View(ee);
}
View is
#model IEnumerable<trydbconnectMvc.Models.Empoyee>
#{
ViewBag.Title = "mytry";
}
<h2>mytry</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
#Html.DisplayNameFor(model => model.LastName)
</th>
<th>
#Html.DisplayNameFor(model => model.MiddleName)
</th>
<th>
#Html.DisplayNameFor(model => model.Address)
</th>
<th>
#Html.DisplayNameFor(model => model.Salary)
</th>
<th>
#Html.DisplayNameFor(model => model.Email)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
#Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
#Html.DisplayFor(modelItem => item.MiddleName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Address)
</td>
<td>
#Html.DisplayFor(modelItem => item.Salary)
</td>
<td>
#Html.DisplayFor(modelItem => item.Email)
</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 am getting error
" The model item passed into the dictionary is of type 'trydbconnectMvc.Models.Empoyee', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[trydbconnectMvc.Models.Empoyee]'."
I am new to mvc. How to solve this?
It seems you're returning only one entity, not a IEnumerable. Just change it in your view:
#model trydbconnectMvc.Models.Empoyee
#{
ViewBag.Title = "mytry";
}
<h2>mytry</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
#Html.DisplayNameFor(model => model.LastName)
</th>
<th>
#Html.DisplayNameFor(model => model.MiddleName)
</th>
<th>
#Html.DisplayNameFor(model => model.Address)
</th>
<th>
#Html.DisplayNameFor(model => model.Salary)
</th>
<th>
#Html.DisplayNameFor(model => model.Email)
</th>
<th></th>
</tr>
<tr>
<td>
#Html.DisplayFor(modelItem => #Model.FirstName)
</td>
<td>
#Html.DisplayFor(modelItem => #Model.LastName)
</td>
<td>
#Html.DisplayFor(modelItem => #Model.MiddleName)
</td>
<td>
#Html.DisplayFor(modelItem => #Model.Address)
</td>
<td>
#Html.DisplayFor(modelItem => #Model.Salary)
</td>
<td>
#Html.DisplayFor(modelItem => #Model.Email)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=#Model.Id }) |
#Html.ActionLink("Details", "Details", new { id=#Model.Id }) |
#Html.ActionLink("Delete", "Delete", new { id=#Model.Id })
</td>
</tr>
}
</table>
or if you want, just change in your Controller:
public ActionResult mytry()
{
Empoyee ee = new Empoyee();
ee.Address = "address1";
ee.Email = "email1";
ee.FirstName = "fname";
ee.Id = 23;
ee.LastName = "lname1";
ee.MiddleName = "";
ee.Salary = 20000;
db.Empoyees.Add(ee);
db.SaveChanges();
var list = new List<Empoyee>(){ ee };
return View(list );
}
and keep your current view.
If you must use an IEnumerable, you can make a List<> and pass it in
public ActionResult mytry()
{
Empoyee ee = new Empoyee();
ee.Address = "address1";
ee.Email = "email1";
ee.FirstName = "fname";
ee.Id = 23;
ee.LastName = "lname1";
ee.MiddleName = "";
ee.Salary = 20000;
db.Empoyees.Add(ee);
db.SaveChanges();
List<Employee> employees = new List<Employee>();
employees.add(ee);
return View(employees);
}

Templates can be used only with field access, property access in Razor

I got this error when I was creating a Edit and Templating for Product.
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
Edit In my Razor View:
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.Select(m => m.ProductID))
</th>
<th>
#Html.DisplayFor(model => model.Select(m => m.Name))
</th>
<th>
#Html.DisplayFor(model => model.Select(m => m.ListPrice))
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.ProductID)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.ListPrice)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.ProductID }) |
#Html.ActionLink("Details", "Details", new { id=item.ProductID }) |
#Html.ActionLink("Delete", "Delete", new { id=item.ProductID })
</td>
</tr>
}
</table>
Do you know what that mean and how to fix it? Thank you so much!
doesnt this just work?
<th> #Html.DisplayFor(model => model.ProductID) </th>
edit, try something like this:
<table id="myTable">
<tr>
<th>
#Html.DisplayNameFor(model => model.ProductID)
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.ProductID)
</td>
<td>
#Html.DisplayFor(modelItem => item.ProductID)
</td>
</tr>
}
</table>

ASP.NET MVC join tables view result error

im trying to join two table and show the results in a strongly-typed list view in with columns from two tables,
public ActionResult OrderDetails(int id) {
var query = from o in db.OrderDetails
join a in db.Albums on o.AlbumId equals a.AlbumId
where o.OrderId == id
select new OrderDetail() {
OrderId = o.OrderId,
AlbumId = o.AlbumId,
Quantity = o.Quantity,
UnitPrice = o.UnitPrice,
Album = new Album {
Title = a.Title
}
};
return View(query);
}
Here is my view
#model IEnumerable<QWERK.OrderDetail>
#{
ViewBag.Title = "OrderDetails";
}
<h2>OrderDetails</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.OrderId)
</th>
<th>
#Html.DisplayNameFor(model => model.AlbumId)
</th>
<th>
#Html.DisplayNameFor(model => model.Quantity)
</th>
<th>
#Html.DisplayNameFor(model => model.UnitPrice)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.OrderId)
</td>
<td>
#Html.DisplayFor(modelItem => item.AlbumId)
</td>
<td>
#Html.DisplayFor(modelItem => item.Quantity)
</td>
<td>
#Html.DisplayFor(modelItem => item.UnitPrice)
</td>
<td>
#Html.DisplayFor(modelItem => item.Album.Title)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.OrderDetailId }) |
#Html.ActionLink("Details", "Details", new { id=item.OrderDetailId }) |
#Html.ActionLink("Delete", "Delete", new { id=item.OrderDetailId })
</td>
</tr>
}
</table>
But i keep getting a NotSupportedException
Im pretty sure im getting the error because im using the OrderDetails Model but how can i get around this to show multiple columns from different tables???? any help would be greatly appreciated...thanks

LINQ query not working properly

I have MVC application and I have two entities.
Entity Lead inherits from Customer.
Now, In Lead controllers index view, I have written below code...
public ViewResult Index()
{
var Leads = (from c in db.Customers.OfType<CRMEntities.Lead>()
where(c.IsActive == true) select c);
return View(Leads.ToList());
}
and I have the code below in the index view.
#model IEnumerable<CRMEntities.Lead>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
.
.
Records exist in the table it not returning any record.
Is query right?
Full View Code
#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>
Query looks fine from what can be seen.
The view code you are showing will not display anything. Write out #Model.Count() in the view to see how many records you have.
If you don't have a database profiler, I'd recommend getting one.

Resources