I keep getting an error message along the lines of:
..but this dictionary requires a model item of type..
My view I'm trying to put the partial view into is:
#using TheNovelMachine.Models
#model TheNovelMachine.Models.Account
#{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<img id="homeimage" src="~/Image/front-page-book.jpg" alt="The Novel Machine" />
<div id="text" class="col-lg-12">
<h1>Profile</h1>
<div class="col-lg-6 no-gutter">
<h2>#Html.DisplayFor(model => model.Username)</h2>
<h4>#Html.DisplayFor(model => model.FullName)</h4>
<p>#Html.DisplayFor(model => model.Email)</p>
</div>
<div class="col-lg-6 pull-right text-right no-gutter">
<img class="profilepic" src="~/Image/#(Html.DisplayFor(model => model.Username)).jpg" />
</div>
<div class="col-lg-12 no-gutter">
<hr/>
<h4>#Html.DisplayFor(model => model.Username)'s Novels</h4>
#{
Html.RenderPartial("~/Views/Shared/_NovelProfile.cshtml");
}
</div>
</div>
And the Partial View is:
#using System.Web.DynamicData
#using TheNovelMachine.Models
#model IEnumerable<TheNovelMachine.Models.Novel>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Title)
</th>
<th>
#Html.DisplayNameFor(model => model.Genre)
</th>
<th>
#Html.DisplayNameFor(model => model.Abstract)
</th>
<th>
#Html.DisplayNameFor(model => model.Privacy)
</th>
<th>
#Html.DisplayNameFor(model => model.AccountId)
</th>
</tr>
#foreach (var item in Model.Where(i => i.AccountId.ToString() == Url.RequestContext.RouteData.Values["id"].ToString())) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
#Html.DisplayFor(modelItem => item.Abstract)
</td>
<td>
#Html.DisplayFor(modelItem => item.Privacy)
</td>
<td>
#Html.DisplayFor(modelItem => item.AccountId)
</td>
</tr>
}
</table>
Your partial view is expecting this model (you are declaring it at the top of you partial view)
#model IEnumerable<TheNovelMachine.Models.Novel>
You should pass that model in this line:
#{
Html.RenderPartial("~/Views/Shared/_NovelProfile.cshtml", Model.Novels);
}
That should work, assuming Model.Novels is of this type IEnumerable<TheNovelMachine.Models.Novel> or it can be converted to that type.
Here you have an example with more details, but the idea is the same. If you don't pass this as a parameter, then the partial view can't be constructed (that's why you are getting that error).
MSDN Documentation here for RenderPartial method.
So even though there are other answers as to what you need to do, here is what is actually happening, so you know in the future.
#Html.RenderPartial("_NovelProfile");
would be the same code as:
#Html.RenderPartial("_NovelProfile", Model)
Related
#model IEnumerable<Calendar.Models.CheckDays
<p>
#using (Html.BeginForm())
{
<table>
<tr>
<th>
#Html.ActionLink("Create New", "Create")
</th>
<th>
#Html.DropDownListFor(model => model.DayOfWeek, htmlAttributes: new { #class = "form-control" })
</th>
<th>
<input type="button" value="Search" />
</th>
</tr>
</table>
}
</p>
<tr>
<th>
#Html.DisplayNameFor(model => model.DayOfWeek)
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.DayOfWeek)
</td>
</td>
</tr>
}
I'm trying to create a drop down list so I can filter the the index results but I keep getting an error
"Compiler Error Message: CS1061: 'IEnumerable' does not
contain a definition for 'DayOfWeek' and no extension method
'DayOfWeek' accepting a first argument of type
'IEnumerable' could be found (are you missing a using
directive or an assembly reference?)"
The line that errors out is
Html.DropDownListFor(model => model.DayOfWeek, htmlAttributes: new { #class = "form-control" })".
Do I need to do something in the model or do I have syntax errors?
You misunderstood what first parameter is for. It is to point where selected item should go for. It is for selectedItem variable. Check this blog post: https://odetocode.com/blogs/scott/archive/2013/03/11/dropdownlistfor-with-asp-net-mvc.aspx. You have to create separate class for model with list of elements and variable SelectedDayOfWeek.
Model class:
public class CheckDaysViewModel
{
public IEnumerable<CheckDays> CheckDays {get;set;}
public IEnumerable<SelectListItem> CheckDaysAsSelectedList => this.CheckDays.Select(e => new SelectListItem(e.DayOfWeek, e.DayOfWeek));
public CheckDays SelectedDay {get;set;}
}
cshtml
#model CheckDaysViewModel
<p>
#using (Html.BeginForm())
{
<table>
<tr>
<th>
#Html.ActionLink("Create New", "Create")
</th>
<th>
#Html.DropDownListFor(m => m.SelectedDay, Model.CheckDaysAsSelectedList, null, htmlAttributes: new { #class = "form-control" })
</th>
<th>
<input type="submit" value="Search" />
</th>
</tr>
</table>
}
</p>
<table>
<tr>
<th>
#Html.DisplayNameFor(m => m.CheckDays.First().DayOfWeek)
</th>
</tr>
#foreach (var item in Model.CheckDays)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.DayOfWeek)
</td>
</tr>
}
</table>
I have this partial View as below:
#model IPagedList<Student>
<h2>List of Students</h2>
<div id="studentList">
<div class="pagedList" data-uwa-target="#studentList">
#Html.PagedListPager(Model,page=>Url.Action("Index",new { page }),
PagedListRenderOptions.MinimalWithItemCountText)
</div>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.LastName) //here error
</th>
<th>
#Html.DisplayNameFor(model => model.FirstName) //here error
</th>
<th>
#Html.DisplayNameFor(model => model.City) //here error
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
#Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
#Html.DisplayFor(modelItem => item.City)
</td>
</tr>
}
</table>
<div class="row">
<div class="col-sm-6" id="slid">
<button id="export" class="btn btn-default btn-primary" onclick="location.href='#Url.Action("ExportInCSVFormat")';return false;">Export Student List in CSV Format</button>
</div>
<div class="col-sm-4" id="excelSlide">
<button id="excelExport" class="btn btn-default btn-success" onclick="location.href='#Url.Action("ExportStudentsInExel")';return false;">Export Student List in Excel Format</button>
</div>
</div>
My Index.cshmtl is :
#model IPagedList<Student>
#{
ViewBag.Title = "Index";
}
#Html.Partial("_Student", Model)
My StudentController is
public ActionResult Index(int page=1)
{
var model = dbAllStudents.Students
.OrderByDescending(p => p.LastName)
.ToPagedList(page, 10);
return View(model);
}
The problem happens to "#Html.DisplayNameFor(model => model.LastName)". It is saying that " IPaged does not contatin a definition for LastName.
I have an idea that the problem is caused because to the Index method of Student Controller I need to add .Select but I am not sure for that. I would really appreciate if someone can help me
It's not working, because overloaded method which you want to use doesn't fit with your interface. It's for IEnumerable<> types. You can access to the first element in the list and it should returns you correct value even if list is empty.
Change your table definition to:
<table class="table">
<tr>
<th>
#Html.DisplayNameFor( => Model[0].LastName)
</th>
<th>
#Html.DisplayNameFor(_ => Model[0].FirstName)
</th>
<th>
#Html.DisplayNameFor(_ => Model[0].City)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(_ => item.LastName)
</td>
<td>
#Html.DisplayFor(_ => item.FirstName)
</td>
<td>
#Html.DisplayFor(_ => item.City)
</td>
</tr>
}
</table>
and everything will be ok.
I am a novice in asp.net and i want create simple database application.
I need pass parameters between view and controller to retrieve data from database.
i need only this data which title is "something". I create simple left menu which contains search settings.
This is my view page.
#model IEnumerable<TwojaBiblioteka.Models.Ksiazka>
#{
ViewBag.Title = "Home Page";
}
#Styles.Render("~/Content/css")
<div class="jumbotron">
<script src="~/Scripts/jquery-2.1.4.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<div class="container">
<div class="row">
<div class="col-md-2">
<h2>Szukaj</h2>
<div class="textboxes">
<input type="text" name="Tytul" class="form-control" id="Tytul" placeholder="Tytuł..." />
<input type="text" name="txtAutor" class="form-control" id="txtAutor" placeholder="Autor..." />
<input type="text" name="txtISBN" class="form-control" id="txtISBN" placeholder="ISBN..." />
</div>
<center>
#Ajax.ActionLink("Szukaj", "SzukajKsiazki", new AjaxOptions()
{
HttpMethod="GET",
UpdateTargetId= "divKsiazki",
InsertionMode= InsertionMode.Replace
})
</center>
</div>
<div id="divKsiazki"class="col-md-10 ">
</div>
</div>
</div>
</div>
This is view for display data from database:
#model IEnumerable<TwojaBiblioteka.Models.Ksiazka>
<table class="table" style="border:1px solid black;">
<tr>
<th>
#Html.DisplayNameFor(model => model.Tytul)
</th>
<th>
#Html.DisplayNameFor(model => model.Autor)
</th>
<th>
#Html.DisplayNameFor(model => model.ISBN)
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Tytul)
</td>
<td>
#Html.DisplayFor(modelItem => item.Autor)
</td>
<td>
#Html.DisplayFor(modelItem => item.ISBN)
</td>
</tr>
}
</table>
And this is my controller:
public PartialViewResult SzukajKsiazki()
{
string tytul="something";
var ksiazkilist = db.Ksiazka.Where(x => x.Tytul == tytul).ToList();
return PartialView("_ListaKsiazek",wypozyczone);
}
So how i can pass data from my textboxes to controller to display only those records which contains textbox text?
Your controller action should accept a parameter. In Asp.Net MVC, it is normal for this to be the model:
public PartialViewResult SzukajKsiazki(IEnumerable<TwojaBiblioteka.Models.Ksiazka> model)
Your view should have all of the editor elements from the model enclosed in a form and you need a submit button:
#using (Html.BeginForm("SzukajKsiazki", "ControllerName", FormMethod.Post)
{
<table class="table" style="border:1px solid black;">
<tr>
<th>
#Html.DisplayNameFor(model => model.Tytul)
</th>
<th>
#Html.DisplayNameFor(model => model.Autor)
</th>
<th>
#Html.DisplayNameFor(model => model.ISBN)
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Tytul)
</td>
<td>
#Html.DisplayFor(modelItem => item.Autor)
</td>
<td>
#Html.DisplayFor(modelItem => item.ISBN)
</td>
</tr>
}
</table>
<input type="submit" value="submit">
}
Let's say i have this
#using MvcApplication6.Models;
#model List<Employee>
I
I have 5 rows/records of Employee and i passed it to my view. They have id, firstname,lastname,gender,department.
I know that i can use foreach to loop through all of them but what if i want to access a specific record from the list?
I get indexing error if i do something like this :
#Html.LabelFor(model => model[0].firstname)
I just want to display a specific record from my page. What should i do to achieve that result?
full code
#using MvcApplication6.Models;
#model IEnumerable<Employee>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New record", "Create")
</p>
#Html.LabelFor(model => model.Model[0].firstname)
<p>#Html.ActionLink("Check departments", "Index", "Department")</p>
<table border="1">
<tr>
<th>
#Html.DisplayNameFor(model => model.id)
</th>
<th>
#Html.DisplayNameFor(model => model.firstname)
</th>
<th>
#Html.DisplayNameFor(model => model.lastname)
</th>
<th>
#Html.DisplayNameFor(model => model.gender)
</th>
<th>
#Html.DisplayNameFor(model => model.department)
</th>
<th>Action</th>
</tr>
#foreach (Employee item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.id)
</td>
<td>
#Html.DisplayFor(modelItem => item.firstname)
</td>
<td>
#Html.DisplayFor(modelItem => item.lastname)
</td>
<td>
#Html.DisplayFor(modelItem => item.gender)
</td>
<td>
#Html.DisplayFor(modelItem => item.department)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.id }) |
#Html.ActionLink("Details","Details", "Employee", new { id=item.id },null) |
#Html.ActionLink("Delete", "Delete", new { id=item.id }) |
#Html.DisplayFor(modelItem => item.Players[0])
</td>
</tr>
}
</table>
You are trying to access the property model on an instance of your collection. Instead, simply reference the model by index to access the firstname property:
#Html.LabelFor(m => m[0].firstname)
The lambda expression exposes the Model, which in your case is a collection. In addition, you should resolve your #model to List<> instead of IEnumerable<> to prevent lazy evaluation.
So, I am new to MVC, and I have a partial view with IEnumerable model, in which I have few fields I would like to be editable.
#model IEnumerable<Allocations>
<p>
#Html.ActionLink("Create New", "AddAllocation", "Admin")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Description)
</th>
<th>
#Html.DisplayNameFor(model => model.Percentage)
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
<td>
#Html.EditorFor(modelItem => item.Percentage)
</td>
<td>#Html.HiddenFor(modelItem => item.ID)</td>
<td>
#Html.ActionLink("Save", "EditAllocation", "Admin", null, new { id= item.ID })
</td>
</tr>
}
</table>
I want to be able to pass the entered/changed textbox values to the controller action. I understand, I can do that using JQuery if it wasn't a IEnumerable model view. I might be missing something here, and would appreciate if someone could direct me in the right direction.