Templates can be used only with field access, property access in Razor - asp.net-mvc

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>

Related

Is there a way I can add and bind checkboxes to my MVC Table

I am trying to add a checkbox so I can selects a row from my list and from there use that row to generate a letter.
I was wondering how I would be able to add a checkbox and bind it to the data I have tried a few ways using something similar to:
checkbox(m => m[i].Lesson)
However it would never accept the i in brackets and come up with an error. below is the layout of my index for the lessons model currently.
Here is the Lessons Index:
#model IEnumerable<FinalMusicApp.Models.Lesson>
#{
ViewBag.Title = "Index";
}
<h2>Lessons</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
#using (Html.BeginForm("Index", "Students", FormMethod.Get))
{
//Search options
<b>Search Options: </b> #Html.RadioButton("Option", "Instrument")<text>Instrument</text>#Html.RadioButton("Option", "Tutor")<text>Tutor</text>#Html.RadioButton("Option", "Term")<text>Term</text>#Html.TextBox("Search")
<input type="submit" name="submit" value="Search" />
}
<form method="post">
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.IsChecked)
</th>
<th>
#Html.DisplayNameFor(model => model.LessonDate)
</th>
<th>
#Html.DisplayNameFor(model => model.LessonTime)
</th>
<th>
#Html.DisplayNameFor(model => model.Duration.TimeTaken)
</th>
<th>
#Html.DisplayNameFor(model => model.Instrument.InstrumentName)
</th>
<th>
#Html.DisplayNameFor(model => model.Letter.Paid)
</th>
<th>
#Html.DisplayNameFor(model => model.Student.FullName)
</th>
<th>
#Html.DisplayNameFor(model => model.Tutor.TutorName)
</th>
<th>
CRUD
</th>
<th>
Make a letter
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.CheckBoxFor(modelItem => item.IsChecked)
</td>
<td>
#Html.DisplayFor(modelItem => item.LessonDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.LessonTime)
</td>
<td>
#Html.DisplayFor(modelItem => item.Duration.TimeTaken)
</td>
<td>
#Html.DisplayFor(modelItem => item.Instrument.InstrumentName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Letter.Paid)
</td>
<td>
#Html.DisplayFor(modelItem => item.Student.FullName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Tutor.TutorName)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.LessonId }) |
#Html.ActionLink("Details", "Details", new { id = item.LessonId }) |
#Html.ActionLink("Delete", "Delete", new { id = item.LessonId })
</td>
<td>
#Html.ActionLink("Generate Letter", "Create", "Letters")
</td>
</tr>
}
</table>
</form>
I am trying to add a checkbox so I can selects a row from my list
Do you want something like below?
In the view add a checkbox, for example I use the property CourseName in my model instead, you can use your property:
#Html.CheckBox("CourseName", new { value = item.CourseName })
#Html.DisplayFor(modelItem => item.CourseName)
Then in the controller, we can get CourseName value, and get other property value accord to the CourseName, but we need load the list again, we cannot send the full list from view to controller :
var courserow = model.FirstOrDefault(m => m.CourseName == CourseName);
result:

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 can I get value textbox inside partial view in asp.net mvc

How can I get value of #Html.TextBoxFor(modelItem => item.Sales) in partial view after pass to controller
My partial view :
#model IEnumerable<PG.Admin.Models.Items.ItemViewModel>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.First().ItemId)
</th>
<th>
#Html.DisplayNameFor(model => model.First().Name)
</th>
<th>
#Html.DisplayNameFor(model => model.First().Sales)
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.ItemId)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.TextBoxFor(modelItem => item.Sales)
</td>
</tr>
}
</table>

redirect to a action that expects value in other controller

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:
ویرایش

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