I am creating my first MVC app and one of the pages need to list a series of questions from a table in a database. I have set-up the Model:
public class Audit
{
public DateTime InspectionDate { get; set; }
public string Engineer { get; set; }
public List<AuditGroup> AuditGroups { get; set; }
}
public class AuditGroup
{
public string GroupName { get; set; }
public List<AuditQuestion> AuditQuestions { get; set; }
}
public class AuditQuestion
{
public string Group { get; set; }
public string Question { get; set; }
public string Answer { get; set; }
public string Comments { get; set; }
}
The models contains Lists, I created a page called Create and populated the model with the groups and questions in the groups and these displayed on the page fine. When I answer the questions (fill in a textbox) and press the submit button it calls the controller:
[HttpPost]
public ActionResult Create(Audit newAudit)
{
try
{
// TODO: Add insert logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
The newAudit has data in the Engineer and Inspection Date but the binder is not picking up the Lists on the page. This is a cut down version I have trying to work it out:
#{
ViewBag.Title = "Audit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#model test.Models.Audit
<h2>
Audit</h2>
#using (Html.BeginForm())
{
<fieldset>
#foreach (var AuditGroups in Model.AuditGroups)
{
#Html.EditorFor(x=> AuditGroups.GroupName)
}
</fieldset>
<p>
<input type="submit" value="Create" />
</p>
}
So here I am going through the list and putting them on the page but on submit the list is null. Does anyone know where I am going wrong?
In summary what I am doing is sending a list of question to page, user to fill in and submit the answers but im not getting the answers for any of the Lists.
please have a look at my answer to this question. it contains some blog posts that addressed this issue. Furthermore, read Yarx's comment on the answer in which he provided the link to a post which helped him solve the problem.
Related
This question already has answers here:
Multiple models in a view
(12 answers)
Closed 6 years ago.
I am devolaping a 2nd hand item selling like OLX, I have two models
1: Product_model
2: Customer_model
so i want to use the the product description and customer information in a single view.
Models:
[Table("Product")]
public class ProductModel
{
[Key]
public string ProductId { get; set; }
public string ProductName { get; set; }
public decimal ProductPrice { get; set; }
public string ProductDescription { get; set; }
public string ProductCategory { get; set; }
public string ProductAddress { get; set; }
public string ProductImage1 { get; set; }
public string ProductImage2 { get; set; }
public string ProductImage3 { get; set; }
public string CustomerId { get; set; }
// public DateTime ProductSubTime { get; set; }
}
[Table("Customer")]
public class CustomerModel
{
[Key]
[Required(ErrorMessage = "Please provide Customer ID",
public string CustomerFullName { get; set; }
public int CustomerContact { get; set; }
public string CustomerEmail { get; set; }
public string CustomerGender { get; set; }
public string CustomerAddress { get; set; }
}
My controller:
public ActionResult Index()
{
return View(cm.Products.ToList());
}
My view:
#model IEnumerable<WebApplication11.Models.ProductModel>
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/MyTemplate.cshtml";
}
<div class="col-md-4"style="border: 2px solid yellow" >
<div class="single-product-widget" style="border: 2px solid black">
<h2 class="product-wid-title">Recently Viewed</h2>
View All
#{ int i = 0;}
#foreach (var item in Model)
{
<div class="single-wid-product" style="border: 2px dashed black">
<img src="~/images/#item.ProductImage1" alt="No Photo" class="product-thumb">
<h2>#Html.DisplayFor(model => item.ProductName)</h2>
</div>
}
<div>
</div>
For understanding purpose i deleted some part of view.
Please guide me accessing both models in particular view.
I tried also some methods but not able to get it.
Thanks in advance...
You can create a view model with 2 properties like following code
public class MyModel
{
public List<ProductModel> Products { get; set; }
public List<CustomerModel> Customers { get; set; }
}
In controller:
public ActionResult Index()
{
var model = new MyModel
{
Products = cm.Products.ToList(),
Customers = cm.Customers.ToList()
};
return View(model);
}
In Veiw
#foreach (var item in Model.Products)
{
#*Write your razor code here*#
}
There are several options to access several models in a particular view. I assume that you just want to show some customer information in page corner or something. Your layout view may have a requirement to include this customer info (but it does not matter anyway).
Most preferable option from my point of view is to use child action.
Create CustomerController
public class CustomerController : Controller {
[ChildActionOnly]
public ViewResult CustomerInfo() {
CustomerModel customer = ...
return PartialView("_CustomerPartial", customer);
}
}
.chstml view will have #Html.Action("CustomerInfo", "Customer") where you want customer info to be displayed.
This way you will have a separate CustomerModel creation logic.
Another option is to use ViewData or ViewBag to store secondary model information like customer info.
Controller code:
public ActionResult Index()
{
var products = cm.Products.ToList();
ViewBag.Customer = ... // get customer
return View(products);
}
View code:
#model IEnumerable<WebApplication11.Models.ProductModel>
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/MyTemplate.cshtml";
var customer = (CustomerModel)ViewBag.Customer;
}
#* display customer info *#
#* either { *#
<div>Welcome #customer.CustomerFullName. You email is #customer.CustomerEmail</div>
#* } or { *#
#Html.Partial("_CustomerPartial", customer)
#* } *#
#foreach(var product in Model) {
#* display product info *#
}
Previous option can be changed in a way to use strongly typed model. It is also can be known as view model - usually a class that exists only to store necessary info for the view. In your case these are some customer and products information.
ViewModel:
public class ProductListViewModel {
public CustomerModel Customer { get; set; }
public IEnumerable<ProductModel> Products { get; set; }
}
I am having a twitter bootstraper tab, with 3 simples tabs (Caracteristiques, Certificat and Contrats)
the tabs are ajax load with asp.net mvc partialviews, they are tied with modelViews:
the partials views strongly tied to the type ViewModel:
// Load Certificat
public ActionResult Certificat()
{
var modelStaffs = _twitterTabsModel.GetStaffs();
return PartialView("_Certificat", modelStaffs);
}
// load Contrats
public ActionResult Contrats()
{
var modelJoueur = _twitterTabsModel.GetFirstJoueur();
return PartialView("_Contrats", modelJoueur );
}
the models:
public class TwitterTabModel
{
public ModelJoueur JoueurVM { get; set; }
public IEnumerable<ModelStaff> StaffVM { get; set; }
}
public class ModelStaff
{
public string NomStaff { get; set; }
public string FonctionStaff { get; set; }
}
public class ModelJoueur
{
public string NomJoueur { get; set; }
public string PrenomJoueur { get; set; }
}
the Caracteristiques Tab views:
#model Controls.Models.ViewModel.TwitterTabModel
<h2>Caracteristiques</h2>
#using (Html.BeginForm())
{
.... the tabs code ...
<p>
<input type="submit" value="Save" />
</p>
}
The tabs load fines, what I want to do is to include a submit button on the first razor view tab, it submit all the other models if loaded, however, when I get the post call, all the others models, JoueurVM and StaffVM are empty even though they are loaded. Why is it according to you ?
edit: This is the controller post code, nothing special, just trying to get the twitterTabModel:
[HttpPost]
public ActionResult Tabs(TwitterTabModel model)
{
return View();
}
Thanks
I figure a workaround, I pass the input values back as formcollection, instead of the overall model, well it it' s not clean, but well, it works as i can get all the values posted
Ok, this is weird. I cannot use BindAttribute's Include and Exclude properties with complex type nested objects on ASP.NET MVC.
Here is what I did:
Model:
public class FooViewModel {
public Enquiry Enquiry { get; set; }
}
public class Enquiry {
public int EnquiryId { get; set; }
public string Latitude { get; set; }
}
HTTP POST action:
[ActionName("Foo"), HttpPost]
public ActionResult Foo_post(
[Bind(Include = "Enquiry.EnquiryId")]
FooViewModel foo) {
return View(foo);
}
View:
#using (Html.BeginForm()) {
#Html.TextBoxFor(m => m.Enquiry.EnquiryId)
#Html.TextBoxFor(m => m.Enquiry.Latitude)
<input type="submit" value="push" />
}
Does not work at all. Can I only make this work if I define the BindAttribute for Enquiry class as it is stated here:
How do I use the [Bind(Include="")] attribute on complex nested objects?
Yes, you can make it work like that:
[Bind(Include = "EnquiryId")]
public class Enquiry
{
public int EnquiryId { get; set; }
public string Latitude { get; set; }
}
and your action:
[ActionName("Foo"), HttpPost]
public ActionResult Foo_post(FooViewModel foo)
{
return View(foo);
}
This will include only the EnquiryId in the binding and leave the Latitude null.
This being said, using the Bind attribute is not something that I would recommend you. My recommendation is to use view models. Inside those view models you include only the properties that make sense for this particular view.
So simply readapt your view models:
public class FooViewModel
{
public EnquiryViewModel Enquiry { get; set; }
}
public class EnquiryViewModel
{
public int EnquiryId { get; set; }
}
There you go. No longer need to worry about binding.
IMHO there is a better way to do this.
Essentially if you have multiple models in the view model the post controller's signature would contain the same models, as opposed to the view model.
I.E.
public class FooViewModel {
public Bar BarV { get; set; }
public Enquiry EnquiryV { get; set; }
public int ThisNumber { get; set; }
}
public class Bar {
public int BarId { get; set; }
}
public class Enquiry {
public int EnquiryId { get; set; }
public string Latitude { get; set; }
}
And the post action in the controller would look like this.
[ActionName("Foo"), HttpPost]
public ActionResult Foo_post(
[Bind(Include = "EnquiryId")]
Enquiry EnquiryV,
[Bind(Include = "BarId"])]
Bar BarV,
int ThisNumber
{
return View(new FooViewModel { Bar = BarV, Enquiry = EnquiryV, ThisNumber = ThisNumber });
}
All while the view still looks like this
#using (Html.BeginForm()) {
#Html.TextBoxFor(m => m.EnquiryV.EnquiryId)
#Html.TextBoxFor(m => m.EnquiryV.Latitude)
#Html.TextBoxFor(m => m.BarV.BarId)
#Html.TextBoxFor(m => m.ThisNumber)
<input type="submit" value="push" />
}
Keep in mind, this form will still post Latitude back (the way you had it set up), however since it is not included in the Bind Include string for Enquiry on the post action, the action will not accept the new value in the resultant Enquiry. I'd suggest making latitude either disabled or not a form element to prevent additional posting data.
In any other scenario you can use bind just fine, but for some reason it dislikes the dot notation for complex models.
As a side note, I wouldn't put the bind attribute on the class directly as it can cause other issues like code replication, and doesn't account for certain scenarios where you may want to have a different binding.
(I modified the variable names for some clarity. I am also aware your question is rather dated, however in searching for the answer myself this is the first SO I stumbled upon before trying my own solutions and coming to the one I posted. I hope it can help out other people seeking a solution to the same issue.)
I have the folowing code in my view, however, I can see that I don`t have the values in the controller. What is wrong?
In the view I have,
<%
using (Html.BeginForm())
{%>
<%=Html.TextBox("Addresses[0].Line1") %>
<%=Html.TextBox("Addresses[0].Line2")%>
<%=Html.TextBox("Addresses[1].Line1")%>
<%=Html.TextBox("Addresses[1].Line2")%>
<input type="submit" name="submitForm" value="Save products" />
<%
}
%>
My classes are as follows:
public class Customer
{
public string FirstName { get; set; }
public string Lastname { get; set; }
public List<Address> Addresses { get; set; }
public Customer()
{
Addresses = new List<Address>();
}
}
public class Address
{
public int Line1 { get; set; }
public int Line2 { get; set; }
}
My controller as follows:
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Customer customer)
{
return View();
}
The parameter for your ActionResult is named customer, so the default model binder will be looking for that name in the form by default. I believe if you modify your code to the following it should pick it up:
<%=Html.TextBox("customer.Addresses[0].Line1") %>
<%=Html.TextBox("customer.Addresses[0].Line2")%>
<%=Html.TextBox("customer.Addresses[1].Line1")%>
<%=Html.TextBox("customer.Addresses[1].Line2")%>
Check to ensure your View is bound to the Customer model.
Also, when viewing the web page containing the form, view the source generated by the View to see if the fields are being properly named.
Finally, if none of the above helps, change the parameter in your Index action like so:
public ActionResult Index(FormCollection form)
then you can use the debugger to inspect the FormCollection object that is passed in to see exactly what the View is sending you.
I can't seem to get the value of a select list to populate the value of the parent object.
I'm using a wrapper object to bind to so that I have access to the values needed for the SelectList as well as the object which needs the value.
I'm willing to bet I'm missing something basic but I can't find it.
I have these models:
public class Status
{
public virtual int Id { get; protected set; }
public virtual string Name { get; set; }
public virtual bool IsClosed { get; set; }
public override string ToString()
{
return Name;
}
}
public class Issue
{
public virtual int Id { get; protected set; }
public virtual string Title { get; set; }
public virtual string Description { get; set; }
public virtual Status Status { get; set; }
public virtual DateTime CreatedOn { get; set; }
public virtual DateTime UpdatedOn { get; set; }
}
And a FormViewModel (from NerdDinner examples) to contain the list of Statuses
public class IssueFormViewModel
{
public Issue Issue { get; set; }
public SelectList Statuses { get; set; }
public IssueFormViewModel(Issue issue, IList<Status> statuses)
{
Issue = issue;
Statuses = new SelectList(statuses, "Id", "Name", statuses[1].Id );
}
public IssueFormViewModel() { }
}
My Create Actions on the Controller look like...
public ActionResult Create()
{
IList<Status> statuses;
Issue issue = new Issue();
// NHibernate stuff getting a List<Status>
return View(new IssueFormViewModel(issue,statuses));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(IssueFormViewModel issueFormView)
{ // At this point issueFormView.Issue.Status == null
// ...
}
My Issue object is bound partially except the status using these names in the View:
<p>
<label for="Issue.Title">Title:</label>
<%= Html.TextBox("Issue.Title", Model.Issue.Title)%>
<%= Html.ValidationMessage("Title", "*") %>
</p>
<p>
<label for="Statuses">Status:</label>
<!-- also tried "Issue.Status" -->
<%= Html.DropDownList("Statuses", Model.Statuses)%>
<%= Html.ValidationMessage("Status", "*")%>
</p>
I tried the dropdown list on my computer and it works, you should make sure NHibernate is bringing back more than 1 item as your code is trying to set the selected item to be the second item.
Statuses = new SelectList(statuses, "Id", "Name", statuses[1].Id);
Remember that Lists a re zero based indexed.
As expected - it WAS something simple.
I changed the view to look like:
<label for="Status.Id">Status:</label>
<%= Html.DropDownList("Status.Id", Model.Statuses)%>
<%= Html.ValidationMessage("Status.Id", "*")%>
and the controller signature to take an "issue" (can't bind to a selectlist!)
[AcceptVerbs(HttpVerbs.Post)]
//public ActionResult Create(IssueFormViewModel issueFormView)
public ActionResult Create(Issue issueToAdd)
{
And in my Post-Create action I have an Issue with a Status. Albeit the status is invalid (it only contains the Id). So before commiting the Issue to the db, I set the issueToAdd like this:
issueToAdd.Status = (from p in GetAllStatuses()
where p.Id == issueToAdd.Status.Id
select p).First();
Edit: And it turns out I didn't even need to fetch a "proper" Status object. It's bound to Id and that's good enough.