mvc multiple models in view - c#

I am making an MVC application. I am creating a View that uses a ViewModel and also use database entities in this view passed by the controller.
Controller:
public ActionResult AddGroup(AddGroupViewModel model)
{
ClassDeclarationsDBEntities1 entities=new ClassDeclarationsDBEntities1();
return View(entities.Subjects.ToList());
}
ViewModel:
public class AddGroupViewModel
{
[Required]
[Display(Name = "Subject")]
public string subject_name { get; set; }
[Required]
[Display(Name = "Number of Groups")]
public int qty { get; set; }
}
And finally my view:
#model List<ClassDeclarationsThsesis.Classes.Subject>
#model ClassDeclarationsThsesis.Models.AddGroupViewModel
#{
ViewBag.Title = "Add Groups";
}
<h2>Add Groups to subjects</h2>
#using (Html.BeginForm("AddGroup", "Account", FormMethod.Post, new { #class = "form-horizontal", role = "form" }))
{
#Html.AntiForgeryToken()
<h4>Create new groups.</h4>
<hr />
#Html.ValidationSummary("", new { #class = "text-danger" })
<div class="form-group">
#{
List<SelectListItem> listItems1 = new List<SelectListItem>();
}
#foreach (var subject in Model)
{
listItems1.Add(new SelectListItem
{
Text = subject.name,
Value = subject.name,
Selected = true
});
}
#Html.LabelFor(m => m.subject_name, new {#class = "col-md-2 control-label"})
<div class="col-md-10">
#Html.DropDownListFor(m => m.subject_name, listItems1, new {#class = "form-control"})
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.qty, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.qty, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Submit" />
</div>
</div>
}
As you see, I am trying to use two models in my view. But gives an exception (well how to distinguish them). How do I combine those two models in a view?
Edit:
So I did what suggested in answer, but now I get such exception:
{"The class 'ClassDeclarationsThsesis.Models.Subject' has no parameterless constructor."}
The class looks like this:
namespace ClassDeclarationsThsesis.Models
{
using System;
using System.Collections.Generic;
public partial class Subject
{
private int v;
private int userid;
public Subject(int v, int userid, string name)
{
this.class_id = v;
this.user_id = userid;
this.name = name;
}
public int class_id { get; set; }
public int user_id { get; set; }
public string name { get; set; }
public virtual Group Group { get; set; }
public virtual Subjects_Users Subjects_Users { get; set; }
public virtual Task Task { get; set; }
}
}
How do I solve it?

Since you already have a view model, I'd use that:
#model ClassDeclarationsThsesis.Models.AddGroupViewModel
And simply add a property to that view model for the collection you also want to use:
public class AddGroupViewModel
{
[Required]
[Display(Name = "Subject")]
public string subject_name { get; set; }
[Required]
[Display(Name = "Number of Groups")]
public int qty { get; set; }
public List<Subject> Subjects { get; set; }
}
Then simply create an instance of that from your controller to send to the view:
var entities = new ClassDeclarationsDBEntities1();
var model = new AddGroupViewModel();
model.Subjects = entities.Subjects.ToList();
// set your other properties too?
return View(model);
Then in the view simply refer to the property on the Model instead of the model itself when you need that collection:
#foreach (var subject in Model.Subjects)
Basically, while you can use only one type for your model (since there's only one Model property available to the view in the framework), that type can be anything you like, even a custom view model type that you define.

Related

Why is the DepartmentID value not being properly bound to the view model's property?

Not sure why but the for some reason the view model is not binding the DepartmentId when I hit the submit button, I get an error:
Value cannot be null.
Parameter name: value
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Not sure which value is being null as the code does not break, rather after hitting the submit button that is the message that is being displayed.
I'm assuming it's the departmentId that's not being properly bound to the DepartmentID property in CoursePreReqViewModel.
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: value
View:
#using (Html.BeginForm("Catalog", "Courses", FormMethod.Post, new { #class = "pure-form pure-form-aligned" }))
{
#Html.AntiForgeryToken()
<div class="row">
<div class="col">
<input id="myInput" class="form-control" type="text" placeholder="Search..">
</div>
<div class="col">
#Html.LabelFor(model => model.DepartmentId, htmlAttributes: new { #class = "control-label col-md-2" })
#Html.DropDownListFor(model => model.DepartmentId, Model.DepartmentList, "Department", new { #class = "form-control required", id = "department-list" })
#Html.ValidationMessageFor(model => model.DepartmentId)
</div>
<div class="col">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
}
Controller:
[HttpPost]
public ActionResult Catalog(CoursePreReqViewModel viewModel)
{
DepartmentRepo dRepo;
CoursesRepo cRepo;
MajorPreRequisitesRepo reqRepo;
using (context)
{
dRepo = new DepartmentRepo(context);
cRepo = new CoursesRepo(context);
viewModel.PopulateDepermentSelectList(dRepo.GetAllDepartments());
reqRepo = new MajorPreRequisitesRepo(context);
viewModel.Courses = cRepo.GetAllCoursesAndPreReqsByDepartment(viewModel.DepartmentId);
}
return View(viewModel);
}
View model:
public class CoursePreReqViewModel
{
[Required]
[Display(Name = "")]
public int DepartmentId { get; set; }
public IList<Course> Courses { get; set; }
public IList<MajorPreRequisite> CoursesAndPreReqs { get; set; }
[Display(Name = "Department: ")]
public IList<Department> Departments { get; set; }
public CoursePreReqViewModel() { }
public SelectList DepartmentList
{
get
{
return new SelectList(Departments, "Id", "Name");
}
}
public void PopulateDepartmentSelectList(IList<Department> populatedDepartments)
{
Departments = populatedDepartments;
}
}
Sarthak here is another answer: this is the asp.net mvc fiddler https://dotnetfiddle.net/ARdtvr
!!!Display Name needs to be a value!!!
I can help you keep the code you have, and answer your question.
controller/classes:
public class Course
{
public int CourseId { get; set; }
}
public class MajorPreRequisite
{
public int MajorPreRequisiteId { get; set; }
}
public class Department
{
public int Id { get; set; }
public string Name { get; set; }
}
public class CoursePreReqViewModel
{
//Make sure to comment this out -or- put this field in view
//try this first with your code, before you change code eg using a dictionary
//[Required]
//[Display(Name = "")]
public int DepartmentId { get; set; }
public IList<Course> Courses { get; set; }
public IList<MajorPreRequisite> CoursesAndPreReqs { get; set; }
[Display(Name = "Department: ")]
public IList<Department> Departments { get; set; }
public CoursePreReqViewModel() { }
public Dictionary<string, string> DepartmentList { get; set; }
public void GetAllCoursesAndPreReqsByDepartment(IList<Course> populateCourses)
{
Courses = populateCourses;
}
}
public class HomeController : Controller
{
[HttpPost]
public ActionResult Catalog(CoursePreReqViewModel viewModel)
{
//Put a breakpoint herre to see departmentid of user choice
return View();
}
public ActionResult Index11()
{
CoursePreReqViewModel viewModel = new CoursePreReqViewModel();
Dictionary<string, string> depts = new Dictionary<string, string>();
depts.Add("1", "deptOne");
depts.Add("2", "deptTwo");
viewModel.DepartmentList = depts;
IList<Course> courses = new List<Course>();
courses.Add(new Course { CourseId = 1 });
courses.Add(new Course { CourseId = 2 });
viewModel.GetAllCoursesAndPreReqsByDepartment(courses);
return View(viewModel);
}
view:
#model WebApplication4what2.Controllers.CoursePreReqViewModel
#{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index11</title>
</head>
<body>
#using (Html.BeginForm("Catalog", "Home", FormMethod.Post, new { #class = "pure-form pure-form-aligned" }))
{
<div class="row">
<div class="col">
<input id="myInput" class="form-control" type="text" placeholder="Search..">
</div>
<div class="col">
#Html.LabelFor(model => model.DepartmentId, htmlAttributes: new { #class = "control-label col-md-2" })
#Html.DropDownListFor(model => model.DepartmentId, new SelectList(Model.DepartmentList, "Key", "Value"), Model.DepartmentId)
#Html.ValidationMessageFor(model => model.DepartmentId)
</div>
<div class="col">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
}
</body>
</html>

How to bind an object in a view to the controller?

This may be a dumb question but I'm kind of new with the razor. I'm trying to create a dynamic form. I have a list of object of fields and show them dynamically in my page. But, when I want to save the selected value of my field for a dropdown(example), I don't know how to save the object of my foreach to my model in the controller (I can save my value with no harm).
Index.cshtml:
<div class="row">
#foreach (var buildingBlock in buildingBlocks)
{
<div class="col-sm">
<div class="card">
<div class="card-body">
<h5 class="card-title">#buildingBlock.BuildingBlockTitle</h5>
#foreach (Test.Test.Models.BuildingBlockField buildingBlockField in buildingBlockFields)
{
<div class="form-group">
<label for="companyName">Company Name</label>
//I tried that but it's not working (Obviously :))
#Html.EditorFor(model => buildingBlockField)
#Html.DropDownListFor(model => model.buildingBlockFields[0].Values, buildingBlockField.OptionDetails, "Select Contract", new { #class = "selectpicker", multiple = "multiple" })
</div>
}
</div>
</div>
</div>
}
</div>
BuildingBlockField:
public class BuildingBlockField
{
public int BuildingBlockFieldID{ get; set; }
public int BuildingBlockID { get; set; }
public List<SelectListItem>? OptionDetails { get; set; }
public string FieldTitle { get; set; }
public FieldType Type { get; set; }
public bool IsMultiple { get; set; }
public int[] Values { get; set; }
public string Value { get; set; }
}
model controller:
public class ContractInformationsModel
{
public List<BuildingBlockField> buildingBlockFields { get; set; }
}
HomeController:
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.BuildingBlocks = Models.BuildingBlock.getBuildingBlocks();
ViewBag.BuildingBlockFields = Models.BuildingBlockField.getBuildingBlockFields();
return View();
}
[HttpPost]
public ActionResult generateWordContract(ContractInformationsModel contractInformations)
{
return View("Index");
}
}
I expect to find in my controller object contractInformations to find a list of buildingBlockFields with all the information and not only the value.
Thank you
Edit :
This seems to work but i have to do it for every property and hide then. Is there any other solution ?
#for (var i = 0; i < buildingBlockFields.Count(); i++){
<div class="form-group">
#Html.HiddenFor(model => model.buildingBlockFields[i].BuildingBlockFieldID, new { Value = buildingBlockFields[i].BuildingBlockFieldID })
#Html.HiddenFor(model => model.buildingBlockFields[i].FieldTitle, new { Value = buildingBlockFields[i].FieldTitle })
#Html.HiddenFor(model => model.buildingBlockFields[i].Type, new { Value = buildingBlockFields[i].Type })
#Html.DropDownListFor(model => model.buildingBlockFields[0].Values, buildingBlockFields[i].OptionDetails, "Select Contract", new { #class = "selectpicker", multiple = "multiple" })
</div>
}
Since you are passing ContractInformationsModel model to your view, which has a list of type BuildingBlockField, your html should contain the building block field ID and a "counter" that can identify indexes in that list.
#{
// declare counter
int i = 0
}
#foreach (BuildingBlockField buildingBlockField in buildingBlockFields)
{
<div class="form-group">
<label for="companyName">#buildingBlockField.FieldTitle</label>
#Html.HiddenFor(model=>model.buildingBlockFields[i].BuildingBlockFieldID)
#Html.TextBoxFor(model => model.buildingBlockFields[i].FieldTitle, new { #class = "form-control", Value = #buildingBlockField.FieldTitle })
#Html.DropDownListFor(model => model.buildingBlockFields[i].Values, buildingBlockField.OptionDetails, "Select Contract", new { #class = "selectpicker", multiple = "multiple" })
</div>
#i++
}

Why does my DropDownList repeat same value?

I am getting values from a ViewModel into a view for form fields. In one of my DropDownList the values are correct but in another the value repeats itself instead of changing. What am I doing wrong?
ViewModel:
namespace FulfillmentPortal.ViewModels
{
public class ViewModel
{
[Required(ErrorMessage = "Please select a carrier")]
public List<Carrier> CarrierList { get; set; }
[Required(ErrorMessage = "Please select a service")]
public List<CarrierService> ServiceList { get; set; }
}
}
Controller:
public class FulfillmentController : Controller
{
private CarrierModel db = new CarrierModel();
// GET: Fulfillment
public ActionResult Index()
{
ViewModel vm = new ViewModel
{
CarrierList = db.Carriers.ToList(),
ServiceList = db.CarrierServices.ToList()
};
return View(vm);
}
}
View:
#model FulfillmentPortal.ViewModels.ViewModel
#{
ViewBag.Title = "index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="panel panel-primary">
<div class="panel-heading">REPORT OPTIONS</div>
<div class="panel-body" style="padding-left:35px;">
<form id="processForm" class="form-horizontal" action="~/Fulfillment/Report" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="sel1">Carrier:</label>
#Html.DropDownListFor(model => model.CarrierList, new SelectList(Model.CarrierList, "CarrierId", "CarrierName"), "Select a Carrier",
new { #class = "form-control", #style = "width:auto; margin-bottom:15px;" })
<label for="sel2">Carrier Services:</label>
#Html.DropDownListFor(model => model.ServiceList, new SelectList(Model.ServiceList, "Code", "WebName"), "Select a Service",
new { #class = "form-control", #style = "width:auto; margin-bottom:15px;" })
</div>
</form>
</div>
</div>
Model:
public partial class CarrierModel : DbContext
{
public CarrierModel()
: base("name=CarrierModel")
{
}
public virtual DbSet<Carrier> Carriers { get; set; }
public virtual DbSet<CarrierService> CarrierServices { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
[HttpPost]
public ActionResult Index(ViewModel viewModel)
{
if (ModelState.IsValid)
{
viewModel.CarrierList.ToList();
viewModel.ServiceList.ToList();
}
return View(viewModel);
}
This my controller now. Now my view is just erroring out. I think I am missing something or misunderstanding something.
DropDownListFor(model => model.ServiceList
This is not how this method is supposed to work, and I suspect this is the answer to your problem.
This lambda is supposed to provide a field that will hold a value that this drop down list outputs. For your case you should have two fields in the model:
public class ViewModel
...
public int CarrierId { get; set; }
public string CarrierServiceCode { get; set; }
These will hold currently selected value, on none if nothing is selected (yet). And they are supposed to be used in that lambda:
DropDownListFor(model => model.CarrierServiceCode
Alternatively you could use DropDownList() method, which does not require a field in the model, and give it a custom name that will be posted with selected value.
Your ViewModel should be as follows:
public class ViewModel
{
[Required(ErrorMessage = "Please select a carrier")]
public int CarrierId {get; set;}
[Required(ErrorMessage = "Please select a service")]
public int ServiceCode {get; set;}
public List<Carrier> CarrierList { get; set; }
public List<CarrierService> ServiceList { get; set; }
}
Then in the view:
<label for="sel1">Carrier:</label>
#Html.DropDownListFor(model => model.CarrierId, new SelectList(Model.CarrierList, "CarrierId", "CarrierName"), "Select a Carrier",
new { #class = "form-control", #style = "width:auto; margin-bottom:15px;" })
<label for="sel2">Carrier Services:</label>
#Html.DropDownListFor(model => model.ServiceCode, new SelectList(Model.ServiceList, "Code", "WebName"), "Select a Service",
new { #class = "form-control", #style = "width:auto; margin-bottom:15px;" })
Then your Index Post method should be as follows:
[HttpPost]
public ActionResult Index(ViewModel viewModel)
{
if(ModelState.IsValid)
{
// do whatever you want with `viewModel.CarrierId` and `viewModel.ServiceCode` here
}
viewModel.CarrierList = db.Carriers.ToList();
viewModel.ServiceList = db.CarrierServices.ToList();
return View(viewModel);
}

Null Reference Exception when i use navigation property in model class

I try to add new entity in database in controller action.
This is my model class
public class Product
{
public int ProductID { get; set; }
[Required(ErrorMessage = "Please enter product name")]
public string Name { get; set; }
[Required(ErrorMessage = "Please enter product model")]
public string Model { get; set; }
[Required(ErrorMessage = "Please enter product serial")]
public string Serial { get; set; }
[Required(ErrorMessage = "Please choose dealer")]
public int DealerID { get; set; }
[Required]
public Guid ClientID { get; set; }
[Required(ErrorMessage = "Please choose employee")]
public Guid EmployeeID { get; set; }
public virtual Dealer Dealer { get; set; }
public virtual Client Client { get; set; }
public virtual Employee Employee { get; set; }
[DisplayName("Commercial use")]
public bool UseType { get; set; }
}
This is actions for creating new product in database
public ViewResult Create()
{
PopulateDropDownLists();
var model = new Product();
return View(model);
}
[HttpPost]
public ActionResult Create(Product model)
{
try
{
if (ModelState.IsValid)
{
_repo.GetRepository<Product>().Add(model);
_repo.Save();
TempData["message"] = "Product was successfully created";
return RedirectToAction("List");
}
}
catch (DataException)
{
TempData["error"] =
"Unable to save changes. Try again, and if the problem persists, see your system administrator.";
return View("Error");
}
PopulateDropDownLists();
return View("Create");
}
CreateView has appropriate model type (Product type in this case). Code below
#using System.Web.Mvc.Html
#model STIHL.WebUI.Models.Product
#using (Html.BeginForm())
{
#Html.EditorFor(m => m.Name)
#Html.EditorFor(m => m.Model)
#Html.EditorFor(m => m.Serial)
<div class="form-group">
#Html.LabelFor(m => m.DealerID, "Dealer")
#Html.DropDownListFor(m => m.DealerID, new SelectList((IEnumerable)TempData["Dealers"],"DealerID", "DealerNumber"), string.Empty, new {#class = "form-control"})
#Html.ValidationMessageFor(m => m.DealerID, null, new {#class = "help-block"})
</div>
<div class="form-group">
#Html.LabelFor(m => m.EmployeeID, "Employee",new {#class = "control-label"})
#Html.DropDownListFor(m => m.EmployeeID, new SelectList((IEnumerable)TempData["Employees"],"EmployeeID", "FullName"),string.Empty, new {#class="form-control"})
#Html.ValidationMessageFor(m => m.EmployeeID, null, new {#class = "help-block"})
</div>
<div class ="ok-cancel-group">
<input class="btn btn-primary" type="submit" value="Create" />
#Html.ActionLink("Cancel", "List","Product",new {#class = "btn btn-primary"})
</div>
}
i always get null reference instead model in [HttpPost] action, but if i use ViewModel instead Model everything is ok (ViewModel code below)
public class ProductViewModel
{
public Product Product { get; set; }
}
I think it cause model class has virtual properties, but anyway i don't understand why it's ok when i use ViewModel.
Can anyone answer me?
Thx in advance.
The virtual properties won't change the outcome. The issue is that the view is written to bind to the view model, therefore accepting the model isn't going to work. If you want to use the model; then bind the view to the model.

Pass a model to a partial view?

this is my partial:
#model RazorSharpBlog.Models.MarkdownTextAreaModel
<div class="wmd-panel">
<div id="wmd-button-bar-#Model.Name"></div>
#Html.TextAreaFor(m => m.Name, new { #id = "wmd-input-" + #Model.Name, #class = "wmd-input" })
</div>
<div class="wmd-panel-separator"></div>
<div id="wmd-preview-#Model.Name" class="wmd-panel wmd-preview"></div>
<div class="wmd-panel-separator"></div>
I'm trying to include it like this in my View:
#using (Html.BeginForm())
{
#Html.LabelFor(m => m.Title)
#Html.TextBoxFor(m => m.Title)
#Html.Partial("MarkdownTextArea", new { Name = "content" })
<input type="submit" value="Post" />
}
these are the model classes:
public class MarkdownTextAreaModel
{
[Required]
public string Name { get; set; }
}
public class BlogContentModel
{
[Required]
[Display(Name = "Post Title")]
public string Title { get; set; }
[Required]
[DataType(DataType.MultilineText)]
[Display(Name = "Post Content")]
public string Content { get; set; }
}
What am I doing wrong, how should I do this in order to make my partial reusable?
Your partial expects an instance of the MarkdownTextAreaModel class. So do so, instead of passing an anonymous object which would throw anyways:
#Html.Partial("MarkdownTextArea", new MarkdownTextAreaModel { Name = "content" })
Now this being said a far better solution would be to adapt your view model, so that it contains a reference to MarkdownTextAreaModel and use editor templates instead of partials in your views, just like so:
public class BlogContentModel
{
[Required]
[Display(Name = "Post Title")]
public string Title { get; set; }
[Required]
[DataType(DataType.MultilineText)]
[Display(Name = "Post Content")]
public string Content { get; set; }
public MarkdownTextAreaModel MarkDown { get; set; }
}
then of course readapt the controller serving this view so that it populates the MarkDown of your view model:
public ActionResult Foo()
{
BlogContentModel model = .... fetch this model from somewhere (a repository?)
model.MarkDown = new MarkdownTextAreaModel
{
Name = "contect"
};
return View(model);
}
and then inside your main view simply:
#using (Html.BeginForm())
{
#Html.LabelFor(m => m.Title)
#Html.TextBoxFor(m => m.Title)
#Html.EditorFor(x => x.MarkDown)
<input type="submit" value="Post" />
}
and then in order to follow standard conventions move your partial to ~/Views/YourControllerName/EditorTemplates/MarkdownTextAreaModel.cshtml and now everything will magically come into place as it should.
#using (Html.BeginForm()) {
#Html.LabelFor(m => m.Title) #Html.TextBoxFor(m => m.Title)
#Html.Partial("MarkdownTextArea", new MarkdownTextAreaModel { Name = "content" })
<input type="submit" value="Post" />
}

Categories

Resources