I have a view that lists auditorium names of a theater.
#ModelType IEnumerable(Of dbtheatersinfo.TheaterAuditorium)
<h2>Theater Auditoriums</h2>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(Function(model) model.Theater.TheaterName)
</th>
<th>
#Html.DisplayNameFor(Function(model) model.TheaterAuditoriumName)
</th>
</tr>
#For Each item In Model
#<tr>
<td>
#Html.DisplayFor(Function(modelItem) item.Theater.TheaterName)
</td>
<td>
#Html.DisplayFor(Function(modelItem) item.TheaterAuditoriumName)
</td>
</tr>
Next
</table>
All auditoriums listed here have the same TheaterName, so I would like to display the first instance of TheaterName in the tag (and remove it from the list). I tried:
<h2>Auditoriums for #Html.DisplayFor(Function(model) model.Theater.TheaterName) </h2>
But that gives me, "'Theater' is not a member of 'IEnumerable(of TheaterAuditorium)'." The data is in there; it's displaying within the For Each loop. I just can't figure out how to use it before the loop.
I asked the wrong question, but ended up needing the answer anyway. To do this View properly, I needed a View Model:
Namespace ViewModels
Public Class AuditoriumIndex
Public Property TheaterAuditorium As IEnumerable(Of TheaterAuditorium)
Public Property Theater As Theater
End Class
End Namespace
In the controller:
Dim viewModel = New AuditoriumIndex()
viewModel.TheaterAuditorium = db.TheaterAuditoriums.Where(Function(a) a.TheaterID = id).SortBy("TheaterAuditoriumName")
viewModel.Theater = db.Theaters.Where(Function(a) a.TheaterID = id).SingleOrDefault()
Return View(viewModel)
And the View:
h2>Auditoriums for #Html.DisplayFor(Function(model) model.Theater.TheaterName)</h2>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(Function(model) model.TheaterAuditorium.FirstOrDefault().TheaterAuditoriumName)
</th>
</tr>
#For Each item In Model.TheaterAuditorium
#<tr>
<td>
#Html.DisplayFor(Function(modelItem) item.TheaterAuditoriumName)
</td>
</tr>
Next
</table>
Here I had to use FirstOrDefault() to access the column name outside of the loop.
Related
I am getting this error in one project and the code works perfectly fine on another project. I have tried multiple to duplicate the code as it is. Is there a way to find where the error originated from??
#if (ViewBag.RolesForThisUser != null)
{
<div style="background-color:lawngreen;">
<table class="table">
<tr>
<th>
#Html.DisplayName("Roles For This User")
</th>
</tr>
<tr>
<td>
#foreach (string s in ViewBag.RolesForThisUser) //this line
{
<li>#s</li>
}
</td>
</tr>
</table>
I suspected ViewBag.RolesForThisUser itself already contains a string, neither an array nor collection of strings (e.g. string[] or List<string>), hence using foreach loop is pointless (and string itself contains char[] array which explains why type conversion failed). You can simply display it without foreach:
#if (!string.IsNullOrEmpty(ViewBag.RolesForThisUser))
{
<div style="background-color:lawngreen;">
<table class="table">
<tr>
<th>
#Html.DisplayName("Roles For This User")
</th>
</tr>
<tr>
<td>
#ViewBag.RolesForThisUser
</td>
</tr>
</table>
</div>
}
Or assign a string collection to ViewBag.RolesForThisUser from GET method so that you can use foreach loop, like example below:
Controller
public ActionResult ActionName()
{
var list = new List<string>();
list.Add("Administrator");
// add other values here
ViewBag.RolesForThisUser = list;
return View();
}
View
#if (ViewBag.RolesForThisUser != null)
{
<div style="background-color:lawngreen;">
<table class="table">
<tr>
<th>
#Html.DisplayName("Roles For This User")
</th>
</tr>
<tr>
<td>
#foreach (string s in ViewBag.RolesForThisUser)
{
<p>#s</p>
}
</td>
</tr>
</table>
</div>
}
I have a model, which can represent 3 categories. I want in my view, make 3 different tables for each category with relevant fields. I think for this I need to use partial view with viewmodel for each category.
So my main model is "Ad", which have 3 sub viewmodels (Realty, Auto and Service).
Here the example how I implement Realty action on my home controller:
public ActionResult Realty()
{
var ads = db.Ads.Include(a => a.Realty);
var vm = new List<RealtyViewModel>();
foreach (var ad in ads)
{
vm.Add(new RealtyViewModel
{
Title = ad.Title,
Descirpiton = ad.Descirpiton,
Type = ad.Realty.Type,
NumberOfRooms = ad.Realty.NumberOfRooms
});
}
return PartialView(vm);
}
Then my partial view, looks like this:
#model IEnumerable<OGAS.Areas.Category.ViewModels.RealtyViewModel>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Title)
</th>
<th>
#Html.DisplayNameFor(model => model.Type)
</th>
<th>
#Html.DisplayNameFor(model => model.Descirpiton)
</th>
<th>
#Html.DisplayNameFor(model => model.NumberOfRooms)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.Type)
</td>
<td>
#Html.DisplayFor(modelItem => item.Descirpiton)
</td>
<td>
#Html.DisplayFor(modelItem => item.NumberOfRooms)
</td>
</tr>
}
</table>
Then in my Index page (without using any models), I call partial view like this:
#{Html.RenderPartial("Realty");}
But then I'm getting following error:
An exception of type 'System.NullReferenceException' occurred in App_Web_gdyh352c.dll but was not handled in user code
Could you please advise if this approach is good (calling 3 vms), if yes how to implement this?
Thanks.
Try to replace #{Html.RenderPartial("Realty");} and use #Html.Action("Realty") in this case, as you need to call back to the controller action, in order to create the model for the partial view.
See MVC Html.Partial or Html.Action for more information.
Use this, for .net core and mvc. #Html.Action has been removed from .net core
#await Html.PartialAsync("_YourPartialViewName", YourModel)
i have a problem.
I have my Controller "DashboardNB2Controller", my View "index.cshtml" and i want to integrate a partial view called "_PartialView.cshtml" in my "index.cshtml". Both Views are in the same folder. In my controller, i have the "ActionResult _PartialView" for a databaseoperation in my partial view.
But if I integrate my partial view in my index view, the action result "_PartialView" didn't work. I get no results. The query for my database is correct. I checked this.
Here are my codes
My Controller with the ActionResult for the Partial View
public ActionResult _PartialView()
{
var lastMessages= (from t in db.view_tbl_message
orderby t.Date descending
select t).Take(10);
ViewModelDashboard model = new ViewModelDashboard();
model.view_tbl_message = lastMessages.ToList();
return PartialView("_PartialView", model);
}
My index.cshtml
#model AisWebController.Areas.Statistics.Models.ViewModelDashboard
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<br />
#{Html.Action("_PartialView", "DashboardNB2");}
<br />
And my _PartialView.cshtml
#model WebApplication.Areas.Stats.Models.ViewModelDashboard
<table class="table table-bordered">
<tr>
<th>
Date
</th>
<th>
User
</th>
<th>
Message
</th>
</tr>
#foreach (var item in Model.view_tbl_message)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Date
</td>
<td>
#Html.DisplayFor(modelItem => item.User)
</td>
<td>
#Html.DisplayFor(modelItem => item.Message)
</td>
</tr>
}
</table>
If someone can help - that would be aweseome!
Change
#{Html.Action("_PartialView", "DashboardNB2");}
to
#Html.Action("_PartialView", "DashboardNB2")
You don't need {} brackets after you have # in view for Html extension methods
Look your #Html.DisplayFor it doesn't have any {} brackets.
Same applies for #Html.ActionLink
I am developing MVC app.
I want to create the list in the controller and pass it to the view.
I have written the method in the controller but dont know how to call it form view and display the value which it return.
Method in controller.
public List<Invoice> GetInvoiceList(int Pid)
{
List<Invoice> Invoices = new List<Invoice>();
var InvoiceList = (from i in db.Invoices
where i.PaymentAdviceId == Pid
select i);
Invoices = InvoiceList.ToList();
return (Invoices);
}
View Code
<div class="row-fluid">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Advice No
</th>
<th>
Invoices
</th>
</tr>
</thead>
#foreach (var item in Model)
{
<tbody>
<tr>
<td>
#Html.DisplayFor(modelItem => item.AdviceNo)
</td>
I wan to call the controller method GetInvoiceList here and
want to display list items here...
<td>
</tr>
</tbody>
Add a PartialView to your project that has it's model set to List<Invoice>
then modify your code:
public PartialViewResult GetInvoiceList(int Pid)
{
List<Invoice> Invoices = new List<Invoice>();
var InvoiceList = (from i in db.Invoices
where i.PaymentAdviceId == Pid
select i);
Invoices = InvoiceList.ToList();
return PartialView("partialViewName", Invoices);
}
in your other view:
<tr>
<td>
#Html.DisplayFor(modelItem => item.AdviceNo)
</td>
<td> #Html.Action("GetInvoiceList", new {Pid = item.id})</td>
</tr>
I have a view in my app that uses a #modeltype ienumerable of (xxxxxxx.abc), and uses a for each loop to populate the view. This view has select boxes that show up next to each item in the view. When I post the form to the controller accepting it as a formcollection does not work, and if I accept it like: ByVal abc as abc it says that its an invalid model.. The code is as follows:
<AcceptVerbs(HttpVerbs.Post)>
Function ClassAttendance(ByVal Attendance As Attendance) As ActionResult
If ModelState.IsValid Then
UpdateModel(Attendance)
db.SaveChanges()
Return RedirectToAction("Index")
End If
Return View()
End Function
Any ideas?? Can I somehow use a for each loop in the view without making it ienumerable? If so when It posts to the controller my below code would just about work.. The only value that really matters is the selectlist choice and the id of the record that it was changed for...
The view is:
#ModelTYPE List(Of xxxxx.attendance)
#Code
ViewData("Title") = "Class Attendance Record"
End Code
#Using Html.BeginForm
#<fieldset>
<table>
<tr>
<th>
First Name
</th>
<th>
Last Name
</th>
<th>
Registrant ID
</th>
<th>
Course Status
</th>
<th>
Comments
</th>
</tr>
#For r As Integer = 0 To ViewBag.count
Dim i As Integer = r
#Html.HiddenFor(Function(m) m(i).id)
#<tr>
<td>
#Html.DisplayFor(Function(m) m(i).firstName)
</td>
<td>
#Html.DisplayFor(Function(m) m(i).lastName)
</td>
<td>
#Html.DisplayFor(Function(m) m(i).reg_id)
</td>
<td>
#Html.DisplayFor(Function(m) m(i).Completed_Class)
</td>
<td>
#Html.DropDownList("Completed_Class", New SelectList(ViewBag.courseStatus, "Status", "Status"))
</td>
<td>
#Html.TextBoxFor(Function(m) m(i).Comments, New With {.class = "AttenComment"})
</td>
</tr>
Next
</table>
<p>
<input type="submit" name="submit" />
</p>
</fieldset>
End Using
I have been over countless tut's and posts with no luck at all... I only want to update the model record for each record to the corresponding selected value from the selectlist... Thanks greatly for any and all help..
In brief, this can be done. There are many ways to accomplish this, the easiest by far I found was to have a custom view model which contains an array of the model you want to bulk insert.
public class MultipleAttendance
{
public Attendance[] attendances {get;set;}
}
Then in your View you can loop as such:
#for(int i=0,i<10;i++)
{
#Html.TextBox(attendances[i].firstName)
}
In the controller
[HttpPost]
public ActionResult Create(MultipleAttendance model)
{
// Get a value
model.attendances[0].firstName;
}
Hope it helps :)