I am using a ViewModel to Create a new item in my DB
the ViewModel has only the properties that I want the user to be able to set, and when it is posted back I make a new 'real' object and save it away to the DB.
I am doing this as detailed below
[HttpGet]
public ActionResult Create(int id = 0)
{
var opt = unitOfWork.OptionRepository.GetByID(id);
CreateAvailabilityViewModel model = new CreateAvailabilityViewModel();
model.OptionDescription = opt.Description;
model.CentreCode = opt.CentreCode;
model.OptionID = id;
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CreateAvailabilityViewModel cavm)
{
if (ModelState.IsValid)
{
OptionAvailability newOA = new OptionAvailability();
DateTime now = DateTime.Now;
newOA.ChangedDate = newOA.CreatedDate = now;
newOA.ChangedBy = newOA.CreatedBy = User.Identity.Name;
newOA.DateFrom = cavm.DateFrom;
newOA.DateTo = cavm.DateTo;
newOA.MinNumbers = cavm.MinNumbers;
newOA.MaxNumbers = cavm.MaxNumbers;
newOA.OptionID = cavm.OptionID;
unitOfWork.OptionAvailabilityRepository.Insert(newOA);
unitOfWork.Save();
return RedirectToAction("Detail", "Option", new { id = newOA.OptionID });
}
return View(cavm);
}
and this is the ViewModel
public class CreateAvailabilityViewModel
{
[HiddenInput(DisplayValue = false)]
public int OptionAvailabilityID { get; set; }
[HiddenInput(DisplayValue = false)]
public int OptionID { get; set; }
[Required]
public DateTime DateFrom { get; set; }
[Required]
public DateTime DateTo { get; set; }
[Required]
public int MinNumbers { get; set; }
[Required]
public int MaxNumbers { get; set; }
public string CentreCode { get; set; }
public string OptionDescription { get; set; }
}
the problem I am facing is that when the form is rendered the form fields for the dates and ints are defaulting to 01/01/0001 and 0 instead of being blank. I am using the Html.EditorFor helpers to render the inputs I assume it is because in the HttpGet Create method, when I instantiate the ViewModel it uses the type defaults and then passes them through in the object to the form, but this is not wha tI want to be happening.. do I need to set these properties to DateTime? and/or int? ?
I am pretty sure this is good practice to use but am a bit stumped as to why
can anyone explain what I am doing wrong please
thanks muchly
You can instantiate the dates with whatever values you want.
You could use backing fields in your viewmodel (instead of auto properties) and initialize them:
public class MyViewModel
{
private DateTime _firstDate = new DateTime(12/12/2012);
private DateTime _secondDate = DateTime.Now();
public DateTime FirstDate { get { return _firstDate; } set { _firstDate = value; } }
...
}
Or you could initialize them in the viewmodel's constructor:
public class MyViewModel
{
public MyViewModel(DateTime firstDate)
{
FirstDate = firstDate;
SecondDate = DateTime.Now();
}
public DateTime FirstDate { get; set; }
....
}
Or you could initialize them in your controller; you probably get the idea.
Also consider decorating these members with metadata:
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime FirstDate { get; set; }
Related
I have a LINQ query in my controller that has a join which selects all records. I'm then passing the ReportCompletionStatus.AsEnumerable() model to my view. But I keep getting the fowlling exceptions..
The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1
but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1
I'm setting the model AsEnumerable() and my view is expecting #model IEnumerable so i'm still not sure why it's complaning...
Controller
var ReportCompletionStatus = from r in db.Report_Completion_Status
join rc in db.Report_Category
on r.Report_Category equals rc.ReportCategoryID
select new
{
r.Report_Num,
rc.ReportCategory,
r.Report_Sub_Category,
r.Report_Name,
r.Report_Owner,
r.Report_Link,
r.Report_Description,
r.Last_Published,
r.Previous_Published,
r.Published_By,
r.Previous_Published_By,
r.Last_Edited,
r.Edited_By
};
return View(ReportCompletionStatus.AsEnumerable());
Model
#model IEnumerable<WebReportingTool.Report_Completion_Status>
With your select new, you project to an anonymous type, not to an IEnumerable<WebReportingTool.Report_Completion_Status>
You need to create a ViewModel class (as your projection has data from both Report_Completion_Status and Report_Category) and use it for projection and for your View's model.
class
public class SomeViewModel {
public int ReportNum {get;set;}
public string ReportCategory {get;set;
//etc.
}
projection
select new SomeViewModel
{
ReportNum = r.Report_Num,
ReportCategory = rc.ReportCategory,
//etc.
};
view
#model IEnumerable<SomeViewModel>
By the way, the AsEnumerable is not necessary.
Here's how I got it to work.
Model
public class ReportCategoryListModel
{
public int Report_Num { get; set; }
public string ReportCategory { get; set; }
public string Report_Sub_Category { get; set; }
public string Report_Name { get; set; }
public string Report_Owner { get; set; }
public string Report_Link { get; set; }
public string Report_Description { get; set; }
public Nullable<System.DateTime> Last_Published { get; set; }
public Nullable<System.DateTime> Previous_Published { get; set; }
public Nullable<int> Published_By { get; set; }
public Nullable<int> Previous_Published_By { get; set; }
public Nullable<System.DateTime> Last_Edited { get; set; }
public Nullable<int> Edited_By { get; set; }
}
Controller
var ReportCompletionStatus = from r in db.Report_Completion_Status
join rc in db.Report_Category
on r.Report_Category equals rc.ReportCategoryID
select new ReportCategoryListModel
{
Report_Num = r.Report_Num,
ReportCategory = rc.ReportCategory,
Report_Sub_Category = r.Report_Sub_Category,
Report_Name = r.Report_Name,
Report_Owner = r.Report_Owner,
Report_Link = r.Report_Link,
Report_Description = r.Report_Description,
Last_Published = r.Last_Published,
Previous_Published= r.Previous_Published,
Published_By = r.Published_By,
Previous_Published_By = r.Previous_Published_By,
Last_Edited = r.Last_Edited,
Edited_By = r.Edited_By
};
return View(ReportCompletionStatus);
View
#model IEnumerable<WebReportingTool.Models.ReportCategoryListModel>
I want the StartDate to remain the same date that was initially set when the ticket was first created.
I have removed the fields from the /Ticket/Edit view and have tried to set the StartDate property in the controller so it enters it automatically for the user.
However, I cannot seem to get it to work...
Here is my Ticket model:
public class Ticket
{
[DisplayName("Ticket No.")]
public int TicketID { get; set; }
public int HardwareID { get; set; }
public int StatusID { get; set; }
public int PriorityID { get; set; }
[StringLength(100)]
[DisplayName("Summary")]
public string Summary { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
[DisplayName("Date Created")]
public DateTime StartDate { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
[DisplayName("Due Date")]
public DateTime DueDate { get; set; }
[DisplayName("Hardware Delivered?")]
public Boolean HardwareDelivered { get; set; }
public virtual Employee Employee { get; set; }
public virtual Hardware Hardware { get; set; }
public virtual Status Status { get; set; }
public virtual Priority Priority { get; set; }
public virtual AssignTicket AssignTicket { get; set; }
}
Here is my /Ticket/View Controller (GET and POST) which includes my attempt at setting the StartDate to the existing StartDate that is set for that particular ticket:
//
// GET: /Ticket/Edit/5
public ActionResult Edit(int id = 0)
{
Ticket ticket = db.Tickets.Find(id);
if (ticket == null)
{
return HttpNotFound();
}
ViewBag.HardwareID = new SelectList(db.Hardwares, "HardwareID", "SerialNumber", ticket.HardwareID);
ViewBag.StatusID = new SelectList(db.Statuses, "StatusID", "StatusName", ticket.StatusID);
ViewBag.PriorityID = new SelectList(db.Priorities, "PriorityID", "PriorityName", ticket.PriorityID);
return View(ticket);
}
//
// POST: /Ticket/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Ticket ticket)
{
if (ModelState.IsValid)
{
ticket.StartDate = ticket.StartDate;
db.Entry(ticket).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.HardwareID = new SelectList(db.Hardwares, "HardwareID", "SerialNumber", ticket.HardwareID);
ViewBag.StatusID = new SelectList(db.Statuses, "StatusID", "StatusName", ticket.StatusID);
ViewBag.PriorityID = new SelectList(db.Priorities, "PriorityID", "PriorityName", ticket.PriorityID);
return View(ticket);
}
Any help would be appreciated!
your set in the post is just setting the date to itself. If you want to maintain the StartDate through the process you need to put it in a for helper on the view (except display) and it will post back to the controller
#Html.HiddenFor(x => x.StartDate) // if you don't want to display it or use the display for to show it
using this you won't need to set it to itself. The value form the get will come back to the post
ticket.StartDate = ticket.StartDate;
What exactly are you doing here?
This does absolutely nothing you're overwriting the value of the variable with the value of the same variable?
What exactly happens when you save your ticket upon edit?
Does it auto update to the latest date?
How does that work. you must be writing DateTime.Now somewhere right?
I have a model which holds some details about an account (Name, Id etc) and then it has a type called Transaction, which holds information about the currently selected account transaction. A transaction can then have many transaction lines. So I have a List<TransactionsLine> property.
I am trying to set the value of a Drop down list, using the model, the value being in the List<> property. At the moment, there can, and must, only be one item in the list.
#Html.DropDownListFor(x=>x.Transaction.TransactionLines[0].CategoryId, Model.TransactionReferences.Categories, new {#onchange="populateSubCategory()"})
However, when I run this, the list defaults to the first item in the list.
In debug mode, when I hover the mouse over x.Transaction.TransactionLines[0].CategoryId, it doesn't show me a value. But when hover over the collection, Model.TransactionReferences.Categories, I see it has a valid list. It just won't set the selected value.
Am I doing this wrong?
It works in other drop downs I use, BUT the select value is in the top most level of my model:
#Html.DropDownListFor(x => x.Transaction.ThirdPartyId, Model.TransactionReferences.ThirdParties, new { #class = "cmbThirdParty form-control", #onchange = "populateDefaults()" })
That one works fine.
Note, doing it manually, works:
<select class="form-control" id="cmbCategory" onchange="populateSubCategory()">
<option value="0">Select a One</option>
#foreach (var cat in Model.TransactionReferences.Categories)
{
//var selected = cat.Value == Model.Transaction.TransactionLines[0].CategoryId.ToString() ? "selected" : "";
<option value="#cat.Value">#cat.Text</option>
}
</select>
But doesn't feel like the best way to do it.
Model:
The main model passed to the view:
public class TransactionModel
{
public int BankAccountId { get; set; }
public string BankAccountName { get; set; }
public TransactionContainer Transaction { get; set; }
public TransactionReferenceModel TransactionReferences { get; set; }
public DateTime DefaultDate { get; set; }
}
The TransactionReferenceModel holds all my 'reference' data used to populate drop down lists:
public class TransactionReferenceModel
{
public List<SelectListItem> TransactionTypes { get; set; }
public List<SelectListItem> EntryTypes { get; set; }
public List<SelectListItem> SubCategories { get; set; }
public List<SelectListItem> Categories { get; set; }
public List<SelectListItem> ThirdParties { get; set; }
public List<SelectListItem> CostCentres { get; set; }
}
The TransactionContainer model holds allthe main details about the selected transaction:
public class TransactionContainer
{
public int Id { get; set; }
public int AccountId { get; set; }
public int TransactionTypeId { get; set; }
public string TransactionType { get; set; }
public int EntryTypeId { get; set; }
public string EntryType { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/dd/yyyy}")]
public DateTime TransactionDate { get; set; }
public string ThirdParty { get; set; }
public int ThirdPartyId { get; set; }
public string Account { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "C2")]
public decimal Amount { get; set; }
public string Notes { get; set; }
public string CategoryDisplay { get; set; }
public string CostCentreDisplay { get; set; }
public decimal RunningBalance { get; set; }
public List<TransactionLine> TransactionLines { get; set; }
}
That then holds a list of transaction lines that make up the transaction. The transaction line holds the property I am trying to set the drop down to, which is CategoryId:
public class TransactionLine
{
public int Id { get; set; }
public int TransactionId { get; set; }
public int? CostCentreId { get; set; }
public string CostCentre { get; set; }
public int SubCategoryId { get; set; }
public string SubCategory { get; set; }
public int CategoryId { get; set; }
public string Category { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "C2")]
public decimal Amount { get; set; }
public string Notes { get; set; }
}
And here is how I am populating my model and sending it to the view:
public ActionResult EditTransaction(int? transactionId, int? bankAccountId)
{
// Create the main view object
var model = new TransactionModel
{
Transaction = new TransactionContainer
{
TransactionLines = new List<TransactionLine>()
}
};
if (transactionId != null) // This is an Edit, as opposed to an Add
{
var item = new TransactionService(currentUserId).GetTransaction(transactionId.Value);
// Populate the Reference object used to populate drop downs.
model.TransactionReferences = PopulateReferenceDate(model.TransactionReferences, item.TransactionLines[0].SubCategoryId);
model.BankAccountId = item.AccountId;
model.BankAccountName = item.Account.FullName;
model.DefaultDate = Session["DefaultDate"] != null
? DateTime.Parse(Session["DefaultDate"].ToString())
: DateTime.UtcNow;
model.Transaction.AccountId = item.AccountId;
model.Transaction.Amount = item.Amount;
model.Transaction.TransactionLines.Add(new TransactionLine
{
Id = item.TransactionLines[0].Id,
CategoryId = item.TransactionLines[0].SubCategory.CategoryId,
CostCentreId = item.TransactionLines[0].CostCentreId,
Notes = item.TransactionLines[0].Notes,
Amount = item.TransactionLines[0].Amount,
SubCategoryId = item.TransactionLines[0].SubCategoryId,
TransactionId = model.Transaction.Id
});
model.Transaction.EntryTypeId = item.EntryTypeId;
model.Transaction.Id = transactionId.Value;
model.Transaction.Notes = item.Notes;
model.Transaction.ThirdPartyId = item.ThirdPartyId;
model.Transaction.TransactionDate = item.TransactionDate;
model.Transaction.TransactionTypeId = item.TransactionTypeId;
}
else
{
// Populate the bank account details
var bank = new BankAccountService(currentUserId).GetBankAccountById(bankAccountId.Value);
model.TransactionReferences = PopulateReferenceDate(model.TransactionReferences, null);
model.BankAccountId = bank.Id;
model.BankAccountName = bank.FullName;
model.Transaction.TransactionLines.Add(new TransactionLine
{
TransactionId = model.Transaction.Id // Link the transaction line to the transaction.
});
var transactionDate = Session["DefaultDate"] != null
? DateTime.Parse(Session["DefaultDate"].ToString())
: DateTime.UtcNow;
// Populate the object to hold the Transaction data, so that we can use it and return it in the view.
model.Transaction.TransactionDate = transactionDate;
}
return View(model);
}
I think you should use the SelectList Constructor in your view, in order to indicate the default value, like this:
#Html.DropDownListFor(
x => x.Transaction.TransactionsLines[0].CategoryId,
new SelectList(Model.TransactionReferences.Categories, "Value", "Text", Model.Transaction.TransactionsLines[0].CategoryId)
)
You are not restricted to use List< SelectListItem > for the collections. You can use a List of a specific class also.
This is the Controller Action Method code:
public class HomeController : Controller
{
public ActionResult Index()
{
var m = new AccountModel();
m.Transaction = new Transaction();
m.Transaction.TransactionsLines = new List<TransactionsLine>();
m.Transaction.TransactionsLines.Add(new TransactionsLine() { CategoryId = 2 });
m.TransactionReferences = new TransactionReferences();
m.TransactionReferences.Categories = new List<SelectListItem>()
{ new SelectListItem() { Text = "Cat1", Value = "1" },
new SelectListItem() { Text = "Cat2", Value = "2" },
new SelectListItem() { Text = "Cat3", Value = "3" }
};
return View(m);
}
}
Customers.cs
public partial class Customers
{
public int sno { get; set; }
public string CustomerName { get; set; }
public string CustomerNo { get; set; }
...
// 20 more attribute too...
}
Cities.cs
public partial class Cities
{
public int sno { get; set; }
public string CityName { get; set; }
public string CityPlate { get; set; }
public string CityPhoneCode { get; set; }
}
AddCustomerViewModel.cs
public class AddCustomerViewModel
{
[Required(ErrorMessage = "Şehir seçiniz.")]
[Display(Name = "Şehir")]
public Nullable<int> CityId { get; set; }
// same with Customers.cs
public int sno { get; set; }
[Required(ErrorMessage = "Müşteri adını giriniz!")]
[Display(Name = "Müşteri Adı")]
public string CustomerName { get; set; }
[Required(ErrorMessage = "Müşteri numarası giriniz!")]
[Display(Name = "Müşteri Numarası")]
public string CustomerNo { get; set; }
...
// 20 more attribute too...
}
Controller
[Authorize(Roles = "Administrator")]
public ActionResult AddCustomer()
{
AddCustomerViewModel addCustomerViewModel = new AddCustomerViewModel();
addCustomerViewModel.Cities = entity.Cities;
return View(addCustomerViewModel);
}
[HttpPost]
[Authorize(Roles = "Administrator")]
public ActionResult AddCustomer(AddCustomerViewModel addCustomerViewModel)
{
entity.Customers.Add(GetCustomerFromViewModel(addCustomerViewModel));
entity.SaveChanges();
return View(addCustomerViewModel);
}
I m using a function that is called GetCustomerFromViewModel to convert addCustomerViewModel to Customer like below:
GetCustomerFromViewModel()
private Customers GetCustomerFromViewModel(AddCustomerViewModel addCustomerViewModel)
{
Customers customer = new Customers();
customer.CityId = addCustomerViewModel.CityId;
customer.CreatorUserId = (Guid)System.Web.Security.Membership.GetUser().ProviderUserKey;
customer.CustomerName = addCustomerViewModel.CustomerName;
customer.CustomerNo = addCustomerViewModel.CustomerNo;
customer.Description = addCustomerViewModel.Description;
...
// 20 more attribute too...
return customer;
}
But Customers class have too many variable (customerNo, CustomerName, ...) , So this is the not good way.
When I use DbContextGenerator and Add classes to dataAnnotations and then When I udated the model, dataAnnotations is deleted. (Because DbContext classes are updated, too)
How Can I use ViewModels with DataAnnotations. And effective insert operation to Db? Article, Tutorial, example or advice?
I hope I can explain.
Thanks a lot...
You may take a look at AutoMapper which will simplify the mapping logic between your domain models and view models so that you don't need to manually map each property. Other than that there's nothing wrong with your code. You are already using a view model and have a mapping layer. So your GetCustomerFromViewModel function might become:
private Customers GetCustomerFromViewModel(AddCustomerViewModel addCustomerViewModel)
{
return Mapper.Map<AddCustomerViewModel, Customers>(addCustomerViewModel);
}
or completely get rid of it and directly use the AutoMapper call in your controller action because this function no longer brings much value:
[HttpPost]
[Authorize(Roles = "Administrator")]
public ActionResult AddCustomer(AddCustomerViewModel addCustomerViewModel)
{
var customer = Mapper.Map<AddCustomerViewModel, Customers>(addCustomerViewModel);
entity.Customers.Add(customer);
entity.SaveChanges();
return View(addCustomerViewModel);
}
I'm newbie to MVC architecture.When I'm trying to update, its showing error ,Its totally strange but the data is updating.
The model item passed into the dictionary is of type 'CMS.Domain.Models.Site', but this dictionary requires a model item of type 'CMS.Web.ViewModels.SiteModel'.'.
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.
Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'CMS.Web.ViewModels.SiteModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[CMS.Web.ViewModels.SiteModel]'.
My code looks like:
ViewModels:
namespace CMS.Web.ViewModels
{
public class SiteModel
{
public SiteModel()
{
SiteStatus = new List<SelectListItem>();
}
[Key]
public int ID { get; set; }
[Required(ErrorMessage = "Site Name is required")]
[Display(Name = "Site Name")]
public string Title { get; set; }
[Display(Name = "Require Login")]
public bool RequiresLogin { get; set; }
[Display(Name = "Force HTTPS")]
public bool ForceHTTPS { get; set; }
[Display(Name = "Enable Approval")]
public bool Approval { get; set; }
[AllowHtml]
public IList<SelectListItem> SiteStatus { get; set; }
public bool Deleted { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedOn
{
get { return _createdOn; }
set { _createdOn = value; }
}
private DateTime _createdOn = DateTime.Now;
public string LastUpdatedBy { get; set; }
public DateTime LastUpdatedOn
{
get { return _lastUpdatedOn; }
set { _lastUpdatedOn = value; }
}
private DateTime _lastUpdatedOn = DateTime.Now;
[Display(Name = "Site State")]
public string SiteState { get; set; }
}
}
Model:
namespace CMS.Domain.Models
{
public partial class Site : Model
{
public string Title { get; set; }
public bool Approval { get; set; }
public bool RequiresLogin { get; set; }
public bool ForceHTTPS { get; set; }
public virtual string SiteStatus { get; set; }
public bool Deleted { get; set; }
}
}
Controller:
public ActionResult Index()
{
var _sites = _siterepository.FindAll();
return View(_sites);
}
public ActionResult Add()
{
var model = new SiteModel();
var _SiteStatus = _siterepository.GetSiteStatus();
foreach (var _sitestatus in _SiteStatus)
{
model.SiteStatus.Add(new SelectListItem()
{
Text = _sitestatus.StatusName,
Value = _sitestatus.StatusName.ToString()
});
}
return View(model);
}
[HttpPost]
public ActionResult Add(SiteModel _sitemodel)
{
var model = _sitemodel.ToEntity();
_siterepository.Add(model);
return View(model);
}
public ActionResult Edit(int id)
{
var model = new SiteModel();
var Site = _siterepository.Find(id);
model = Site.ToModel();
var _SiteStatus = _siterepository.GetSiteStatus();
foreach (var _sitestatus in _SiteStatus)
{
model.SiteStatus.Add(new SelectListItem()
{
Text = _sitestatus.StatusName,
Value = _sitestatus.StatusName.ToString(),
Selected = _sitestatus.StatusName == Site.SiteStatus
});
}
return View(model);
}
[HttpPost]
public ActionResult Edit(SiteModel _sitemodel)
{
var model = _sitemodel.ToEntity();
_siterepository.Update(model);
return View(model);
}
I'm struggling to resolve this , please help.
Check your View's model declaration. It is expecting an enumerable list (IEnumerable<CMS.Web.ViewModels.SiteModel>), but you are passing it a single instance of CMS.Web.ViewModels.SiteModel