I have created Viewmodel class and created two properities(by using two model classes i.e from customerModel & addressModel) in that class.Then how to bind the data from view to model.
namespace customer2.Models
{
public class customerdetails {
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { set; get; }
[Key] public int customerid { set; get; }
public string customername { set; get; }
}
public class addressdetails
{
public int addressno { set; get; }
public string street { set; get; }
public string landmark { set; get; }
public int pincode { set; get; }
}
public class MkContext : DbContext
{
public virtual DbSet<customerdetails> customers { get; set; }
public virtual DbSet<addressdetails> address { get; set; }
}
public class customerviewmodel
{
public customerdetails cd { set; get; }
public List<addressdetails> ad { set; get; }
}
}
Create a ViewModel with Combined values of other two data Models for what values we are capture as input in Form
place the ViewModel as a parameter in the controller action method.
Inside the action method, you can bind the values to the two different data models.
HTML Form
<form action='controller/SaveCustomer' method='post'>
<input Type='text' name='FirstName' />
<input Type='text' name='LastName' />
<input Type='text' name='Address' />
</form>
View Model
Public Class CustViewModel {
public string FirstName {get; set;}
public string LastName{get; set;}
public string Address{get; set;}
}
Customer Data Model
Public Class CustModel {
public string FirstName {get; set;}
public string LastName{get; set;}
}
addressModel
Public Class AddModel {
public string Address{get; set;}
}
Controller Method
Public ActionResult SaveCustomer(CustViewModel Details)
{
CustModel Cust = new CustModel();
AddModel Add= new AddModel();
Cust.FirstName= Details.FirstName;
Cust.LastName = Details.LastName;
Add.Address = Details.Address ;
return view();
}
Or Else You Can directly add the model class in Controller
Controller Method
Public ActionResult SaveCustomer(CustModel CustDetails, AddModel AddDetails)
{
//Do the Action
return view();
}
Related
I've done this EF MVC Application (Code First) with listing/editing/deleting functions. Everything works fine, but now I need to add two dropdown fields. Product has a category and a subcategory which needed to be edited. This is what I have so far:
Main class where ProductSubcategoryID is a foreign key
public class Product
{
public int ProductID { get; set; }
public string Name { get; set; }
public string ProductNumber { get; set; }
public int? ProductSubcategoryID { get; set; }
public IEnumerable<SelectList> SelectedCat = new List<SelectList> {};
public IEnumerable<SelectList> SelectedSubCat = new List<SelectList> {};
}
public class ProductCategory
{
public int ProductCategoryID { get; set; }
public string Name { get; set; }
}
public class ProductSubcategory
{
public int ProductSubcategoryID { get; set; }
public int ProductCategoryID { get; set; }
public string Name { get; set; }
}
On the Product controller class I have:
public ActionResult Create()
{
ViewBag.SubcatSelection = new SelectList(dbSubcat.ProductSubcategories, "ProductSubcategoryID", "Name"); ;
return View();
}
and on Edit:
#Html.LabelFor(model => model.ProductSubcategoryID)
#Html.DropDownListFor(model => model.SelectedSubCat, ViewBag.SubcatSelection as SelectList, "ProductSubcategoryID", "Name");
The result:
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'SelectedSubCat'.
I want to use multiple model in one view, I red some article to use complex Model, or Partial View, or Viewbag and other,but couldn't find normal answer.
I want to display data from different models.
Here is my Models
public class Department
{
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
public string Image { get; set; }
public IEnumerable<Teacher> Teachers { get; set; }
}
public class Teacher
{
public int TeacherID { get; set; }
public string TeacherName { get; set; }
public string TeacherLname { get; set; }
public int DepartmentID { get; set; }
public string Image { get; set; }
public Department Department { get; set; }
}
public class ClassOut
{
public int Id { get; set; }
public string ClassOutName { get; set; }
}
public class SchoolDbContext:DbContext
{
public DbSet<Teacher> Teachers { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<ClassOut> ClassOuts { get; set; }
}
Combine all your models in one common view model and use it in your view.
public class ViewModelName
{
public Department Department {get; set;}
public Teacher Teacher {get; set;}
public ClassOut ClassOut {get; set;}
}
Use this model for your view:
#model ViewModelName
......
#Html.TextBoxFor(x => x.Teacher.TeacherName )
.....
And in your post action method
public ActionResult MethodName(ViewModelName viewModel)
{
//
}
Try to create a Class ( for example Manager) where you add a variable for Department, ClassOut, ClassOutName and then you pass the Class Manager to the view and then you can access easily
public class Manager
{
public Department dept;
public ClassOut cl;
public ClassOutName clname;
}
The model is
public class AdminTicket
{
public int Id;
public string Title;
public string Description;
public DateTime CreationDate;
}
The action is
public ViewResult EditTicket(int id)
{
return View(_ticketRepository.Tickets.FirstOrDefault(c => c.Id == id));
}
(I checked, an action give to a view the right model)
The view is
#model TicketSystemMVC5.Models.AdminTicket
#using (Html.BeginForm("EditTicket", "Admin"))
{
#Html.EditorFor(model=>model.Title)
#Html.EditorForModel()
<input type="submit" value="Save" />
#Html.ActionLink("Вернуться к списку", "Index")
}
The result is a form with Title-editor, a button and a link. Where are all the editor fields?
The EditorForModel doesn't like fields, it only renders properties - you can use the "auto-properties" quite happily:
public class AdminTicket {
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public DateTime CreationDate { get; set; }
}
Then your input fields will magically appear.
This is as documented (emphasis added):
Returns an HTML input element for each property in the model.
To test, this I created the following class:
public class AdminTicket {
public int Id { get; set; }
public string Title;
public string Description;
public DateTime CreationDate { get; set; }
}
And EditorForModel resulted in:
I want to join two below model class with entity framework in controller for present factor in accounting system in a view
<pre>
namespace AccountingSystem.Models
{
public class BuyFactor
{
public int BuyFactorId { get; set; }
public DateTime CreateDate { get; set; }
public string Seller { get; set; }
public string Creator { get; set; }
public decimal SumAllPrice { get; set; }
public ICollection<BuyFactorDetail> BuyFactorDetails { get; set; }
}
}
namespace AccountingSystem.Models
{
public class BuyFactorDetail
{
public int BuyFactorDetailId { get; set; }
public int Number { get; set; }
public string Description { get; set; }
public decimal SumPrice { get; set; }
public int BuyFactorId { get; set; }
public virtual BuyFactor BuyFactor { get; set; }
public virtual Commodity Commodity { get; set; }
}
}
</pre>
Create a new Model
public class JointModel
{
public BuyFactor BuyFactor {get; set;}
public BuyFactorDetail BuyFactorDetail {get; set;}
}
Just create another model then calls the other model in there
public class ParentModel
{
public BuyFactor BuyFactor {get; set;}
public BuyFactorDetail BuyFactorDetail {get; set;}
}
So when you will call it in view
#model IEnumerable<AccountingSystem.Models.ParentModel>
#Html.DisplayNameFor(model => model.BuyFactor.Creator)
Best way define Model as a Property in Main Model
For example
public class BuyFactor
{
public int BuyFactorId { get; set; }
public DateTime CreateDate { get; set; }
public string Seller { get; set; }
public string Creator { get; set; }
public decimal SumAllPrice { get; set; }
public ICollection<BuyFactorDetail> BuyFactorDetails { get; set; }
public BuyFactorDetail BuyFactorEntity {get;set;}
}
Assign value in BuyFactorEntity and use as Model.BuyFactorEntity.BuyFactorDetailId
Use a Linq join query like
var query = (from b in context.BuyFactors
join d in context.BuyFactorDetail
on
..
select new
{
BuyFactorId = b.BuyFactorId,
....
BuyFactorDetailId = d.BuyFactorDetailId,
...
..
}));
Your BuyFactor already contains the BuyFactorDetail collection. You sure the entities are 1:N relationship with each other?
You can use the BuyFactor as model and could use the BuyFactorDetails propertu of the BuyFactor entity.
Use ViewBag in controller to assign respective objects for both.
ViewBag.BuyFactor= BuyFactor;
ViewBab.BuyFactorDetail = BuyFactorDetail;
To use it in view you will have to typecast them back.
I have a ViewModel as below:
public class CheckoutViewModel
{
public string ProductNumber { get; set; }
public string Name { get; set; }
public int Price { get; set; }
public Input UserInput;
public class Input
{
public string Email { get; set; }
public string Phone { get; set; }
}
}
And an action like this:
[HttpPost]
public ActionResult Index(CheckoutViewModel model)
{
// ...
return View();
}
And my model has bound as below:
#model GameUp.WebUI.ViewModels.CheckoutViewModel
#using (Html.BeginForm("Index", "Checkout", FormMethod.Post))
{
#Html.AntiForgeryToken()
<!-- some HTML -->
#Html.LabelFor(m => m.UserInput.Email)
#Html.TextBoxFor(m => m.UserInput.Email)
#Html.LabelFor(model => model.UserInput.Phone)
#Html.TextBoxFor(model => model.UserInput.Phone)
<button>Submit</button>
}
When I submit the form, the UserInput is null. I know ASP.NET MVC is able to bind nested types but in this code is not. Also I can get the Email and Phone values by:
var email = Request.Form["UserInput.Email"];
var phone = Request.Form["UserInput.Phone"];
Maybe I do something wrong! It's a simple model binding you can find everywhere in the web.
You forgot to put a setter in your UserInput, I don't think the setter is automatic. Anyway you can make it work by just putting a getter/setter in your UserInput and no need to do extra in your controller method:
public Input UserInput { get; set; }
Your complete model:
public class CheckoutViewModel
{
public string ProductNumber { get; set; }
public string Name { get; set; }
public int Price { get; set; }
public Input UserInput { get; set; }
public class Input
{
public string Email { get; set; }
public string Phone { get; set; }
}
}