Error while inserting floating point value in textboxfor MVC - asp.net-mvc

I have a strange problem, I have a model definied like this:
public class AddEventModel
{
[Required]
[DataType(DataType.Text)]
[Display(Name = "Nazwa wydarzenia")]
public string EventName { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Rodzaj")]
public string EventType { get; set; }
[Required]
[DataType(DataType.DateTime)]
[Display(Name = "Data")]
public System.DateTime EventDate { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Widocznosc")]
public string IsPublic { get; set; }
[DataType(DataType.Text)]
[Display(Name = "Minimalny wiek")]
public int MinimalAge { get; set; }
[Display(Name = "Cena")]
[DataType(DataType.Text)]
public decimal Payment { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Opis")]
public string Description { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Miasto")]
public string City { get; set; }
public SelectList EventTypeList { get; set; }
}
What is more i have a page writen in razor like this (i will post just part of it):
<div class="form-group">
#Html.LabelFor(m => m.EventName, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.EventName, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.EventType, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.DropDownListFor(m => m.EventType, Model.EventTypeList, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.EventDate, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.EventDate, new { #class = "form-control", id = "datepicker" })
</div>
<script type="text/javascript">
$(function () {
$('#datepicker').datetimepicker({
minDate: moment()
});
});
</script>
</div>
<div class="form-group">
#Html.LabelFor(m => m.IsPublic, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
<table>
<tr>
<td><label>#Html.RadioButtonFor(m => m.IsPublic, "Prywatne") Prywatne</label></td>
</tr>
<tr>
<td><label>#Html.RadioButtonFor(m => m.IsPublic, "Publiczne") Publiczne</label></td>
</tr>
</table>
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.MinimalAge, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.MinimalAge, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.Payment, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.Payment, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.Description, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextAreaFor(m => m.Description, new { #class = "form-control" })
</div>
</div>
</div>
Okay, so, when I am inserting my new event with a floating point number in field Cena/Payment im getting a strange error like this:
The ViewData item that has the key 'EventType' is of type 'System.String' but must be of type 'IEnumerable'.
It is pretty strange beacuse it is pointing on dropdownlist which is not connected with a Payment field. As I said, when I put a integer into Payment field - everything works fine.
Whats going on guys?
EDIT
Okey guys, i get it that i didnt reassing a SelectList in a post method. I fixed that already. However:
I want to understand why it was apearing only when i put a floating point number in a textbox
How to fix next problem: Value 'x.x' is not valid for Cena

The error is throw because the value of EventTypeList in
#Html.DropDownListFor(m => m.EventType, Model.EventTypeList, new { #class = "form-control" }
is null
This is happening because when you post back, ModelState is invalid and you return the view without re-assigning the SelectList (as you did in the GET method).
The reason ModelState is invalid is because the culture on the server does not accept the . (dot) character as a decimal separator (most likely it is a culture that uses , (comma) as the decimal separator). You need to change the culture in the web.config file, for example to <<globalization culture ="en-US" />

Related

Display name of Identity User who created and last updated record when ID is saved

I must not be searching with the correct phrases. This is a simple concept and I’ve done it in other languages and frameworks with ease.
I’m saving the UserID for the person who created the record and the UserID who last updated the record. Instead of displaying the UserID, I want to display the User.FirstName + ‘ ‘ + User.LastName.
The way I have it currently the LastEditBy and CreateBy is displayed on the page as blank.
Controller: I get the customer model and manually map the model to the customerViewModel then pass it to my partial view.
public ActionResult Edit(int customerId)
{
Customer customer = DbContext.Customers.FirstOrDefault(x => x.CustomerId == customerId);
CustomerViewModel customerViewModel = MapToViewModel(customer);
customerViewModel.UserSelectList = GetUserGroupList();
UserManager<ApplicationUser> _userManager = HttpContext.GetOwinContext().Get<ApplicationUserManager>();
var CreateByUser = _userManager.FindById(customerViewModel.CreateById);
var EditByUser = _userManager.FindById(customerViewModel.LastEditById);
customerViewModel.CreateBy = CreateByUser.FirstName + " " + CreateByUser.LastName;
customerViewModel.LastEditBy = EditByUser.FirstName + " " + EditByUser.LastName;
if (Request.IsAjaxRequest()) {
return PartialView("_CustomerEditPartial", customerViewModel);
}
return View("_CustomerEditPartial", customerViewModel);
}
The CustomerViewModel:
public class CustomerViewModel : DbContext{
public CustomerViewModel(): base("name=CustomerViewModel")
{
}
[Key]
public int CustomerId { get; set; }
[MaxLength(128), ForeignKey("ApplicationUser")]
public string UserId { get; set; }
public SelectList UserSelectList { get; set; }
#region additional Fields
// This overrides default conventions or data annotations
[Required(ErrorMessage = "Please enter your first name.")]
[StringLength(50)]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Please enter your last name.")]
[StringLength(100)]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime CreateDate { get; set; } = DateTime.Now;
public string CreateById { get; set; }
[NotMapped]
public string CreateBy { get; set; }
public string LastEditById { get; set; }
[NotMapped]
public string LastEditBy { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime LastEditDate { get; set; } = DateTime.Now;
public virtual ApplicationUser ApplicationUser { get; set; }
}
public class UserGroupList
{
public string Value { get; set; }
public string Text { get; set; }
}
My partial view page: _CustomerEditPartial.cshtml
#model WOA.ViewModels.CustomerViewModel
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" daa-dismiss="modal" aria-hidden="True">x</button>
<h4 class="modal-title">Edit Customer</h4>
</div>
#using (Ajax.BeginForm("Edit", "Customers", null, new AjaxOptions { HttpMethod = "Post", OnFailure = "OnFail" }, new { #class = "form-horizontal", role = "form" })) {
<div class="modal-body">
<div class="form-horizontal">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.CustomerId)
<div class="form-group">
#Html.LabelFor(model => model.UserId, "UserId", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.UserId, ViewData.Model.UserSelectList, "Select One", new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.UserId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.FirstName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.FirstName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.LastName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.LastName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.LastName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.CreateDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.CreateDate, new { #readonly = "readonly" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.CreateBy, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.CreateBy, new { #readonly = "readonly" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.LastEditBy, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.LastEditBy, new { #readonly = "readonly" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.LastEditDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.LastEditDate, new { #readonly = "readonly" })
</div>
</div>
</div>
</div>
<div class="modal-footer">
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input type="submit" class="btn btn-primary" value="Save changes" />
</div>
</div>
</div>
<script type="text/javascript">
function OnSuccess() {
alert("success");
}
function OnFail() {
alert("fail");
}
function OnComplete() {
alert("Complete");
}
</script>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
</div>
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
I have updated my code, it is working now, however I do not believe it is the proper way to do this.
I believe I should be able to return the additional values I need via Linq on the initial call and not make two more trips to the database for the additional values.
I have not been able to figure out a way to make this work with Linq.
Thank you in advance for your time and effort.

How to save to database Client IP Address of Visitors Machine in ASP.Net MVC?

am trying to get IP Address of client Machine Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; it's working fine.
Get IP On page load
public ActionResult AddCompany()
{
get_IP();
return View();
}
get_IP code
public void get_IP()
{
string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ipAddress))
{
ipAddress = Request.ServerVariables["REMOTE_ADDR"];
}
ViewBag.IPAddress = ipAddress;
}
But when user register the form get IP address value also save in the data base..
Cs.html File
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
<h1 class="page-header">Add Company</h1>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
#Html.ValidationSummary(true)
#Html.HiddenFor(model => model.IPAddress)
<div class="form-group">
#Html.LabelFor(model => model.CompanyName, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.CompanyName)
#Html.ValidationMessageFor(model => model.CompanyName)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ShortName, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ShortName)
#Html.ValidationMessageFor(model => model.ShortName)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Email, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Email)
#Html.ValidationMessageFor(model => model.Email)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Country, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#*#Html.EditorFor(model => model.Country)*#
#Html.DropDownList("Country", null, "---Select Country----")
#Html.ValidationMessageFor(model => model.Country)
</div>
</div>
<div class="form-group">
<b>State: </b>
<select id="state"></select><br />
</div>
<div>
<b>City: </b>
<select id="city"></select><br />
</div>
<div class="form-group">
#Html.LabelFor(model => model.MobileNo, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.MobileNo)
#Html.ValidationMessageFor(model => model.MobileNo)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
</div>
I create one #Html.HiddenFor(model => model.IPAddress) hidden filed value but it's not working ..
On page load IPAddress Value is showing .after i click to save button the Ip value is not saved to the database..
AddCompany
[HttpPost]
public ActionResult AddCompany(Company cmp)
{
using (DataContext entities = new DataContext())
{
entities.Company.Add(cmp);
entities.SaveChanges();
int id = cmp.CompanyID;
Session.Clear();
Response.Redirect("Index");
}
return View(cmp);
}
Any idea How to save client IP Adrress from the database..
Class File
[Table("MySecondMVCDemo")]
public class Company
{
[Key]
public int CompanyID { get; set; }
[Required(ErrorMessage = "Please Enter Company Name")]
[Display(Name = "CompanyName")]
public string CompanyName { get; set; }
[Required(ErrorMessage = "Please Enter Short Name")]
[Display(Name = "ShortName")]
public string ShortName { get; set; }
[Required(ErrorMessage = "Please Enter Email Address")]
[Display(Name = "Email")]
public string Email { get; set; }
[Required(ErrorMessage = "Please Enter Email Address")]
[Display(Name = "Country")]
public string Country { get; set; }
[Required(ErrorMessage = "Please Enter Mobile No")]
[Display(Name = "MobileNo")]
public string MobileNo { get; set; }
public Int32? IPAddress { get; set; }
}
I do not know about HiddenFor because I have not use it before.
How I hid fields before was like this:
#Html.TextBoxFor(model => model.IPAddress, new { #class = "sr-only", #type = "hidden" })
You can add the bootstrap class sr-only to make sure it is hidden.
Make sure you check your browser inspector to see whether the hidden field has a value.
If posted and is still not saving, then try to assign the IpAddress identity to the form field value before saving it like this:
cmp.IPAddress = Request.Form["IPAddress"];
Store IP address as a string instead of Int32
Please use code from here to get IP Address
http://tutorialgenius.blogspot.in/2010/09/aspnet-get-ipv4-address-even-if-user-is.html

StartTime less than EndTime validation not working using foolproof

I have two date fields that I want to make sure that the end date is greater than the start date. It seems to work but not always. When testing out with different dates it stops working. I am using foolproof. Here is the code
#using (Html.BeginForm("CreateMeeting", "Home", FormMethod.Post, new { #class = "form-horizontal", role = "form" }))
{
#Html.AntiForgeryToken()
<h3>Book a new visit</h3>
<div>The information entered below will be sent to the visitors email address #Model.Info.Email</div>
<p> </p>
#Html.ValidationSummary("", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(m => m.NewMeeting.Title, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.NewMeeting.Title, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.NewMeeting.StartTime, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.NewMeeting.StartTime, new { #class = "datetimepicker form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.NewMeeting.EndTime, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.NewMeeting.EndTime, new { #class = "datetimepicker form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
#Html.HiddenFor(m => m.NewMeeting.SubjectId)
#Html.HiddenFor(m => m.NewMeeting.FirstName)
#Html.HiddenFor(m => m.NewMeeting.LastName)
#Html.HiddenFor(m => m.NewMeeting.Email)
#Html.HiddenFor(m => m.NewMeeting.HostEmail)
#Html.HiddenFor(m => m.NewMeeting.HostMobile)
<input type="submit" class="btn btn-default" value="Send Invite" />
</div>
</div>
}
The model is declared
public class Meeting
{
[Key]
public int Id { get; set; }
[Required]
[Display(Name = "Reason for invitation")]
public string Title { get; set; }
[Required]
[Display(Name = "Start Time")]
[DataType(DataType.DateTime)]
public DateTime StartTime { get; set; }
[Required]
[Display(Name = "End Time")]
[DataType(DataType.DateTime)]
[GreaterThan("StartTime")]
public DateTime EndTime { get; set; }
public string HostEmail { get; set; }
public string HostMobile { get; set; }
public NotificationMethod HostNotificationMethod { get; set; }
public bool HostAlreadyNotified { get; set; }
}
Added the script as a bundle
bundles.Add(new ScriptBundle("~/bundles/foolproof").Include(
"~/Client Scripts/mvcfoolproof.unobtrusive.js"));
And into the _Layout.cshtml page
#Scripts.Render("~/bundles/jquery")
#Scripts.Render("~/bundles/jqueryval")
#Scripts.Render("~/bundles/foolproof")
Any ideas whats going on. Seems like first time I pick a date where end date is less than start date, the validation works. However when I start playing around with different dates to break the system it stops working and the form is submitted with end date less than start date. I have even got it sometimes to a state where the end date is greater than the start date but the validation still fails as it thinks the start date is greater still.
See screenshot, after playing with the end and start dates I can get into states like this:
https://postimg.org/image/rki9qfmtz/

modelstate.isvalid not getting all the errors

I have this model :
[Required(ErrorMessage = "Please provide a valid EmailAddress")]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required(ErrorMessage = "Please provide a company name")]
[Display(Name = "Company")]
public string CompanyName { get; set; }
[Required(ErrorMessage = "Please provide a username")]
[Display(Name = "Username")]
public string UserName { get; set; }
[Required(ErrorMessage = "Please select at least one language")]
public int[] SelectedLanguages { get; set; }
[Required(ErrorMessage = "Please select at least one business unit")]
public int[] SelectedBusinessUnits { get; set; }
Now when I do a post from my form using this model and I don't provide any of the values, I only get errormessages for Email, Company and UserName.
I don't get messages for the SelectedLanguages or the SelectedBusinessUnits.
What am i doing wrong?
THis is the view
#using (Html.BeginForm("Register", "Account", FormMethod.Post, new { #class = "form-horizontal", role = "form" }))
{
#Html.AntiForgeryToken()
<h4>Create a new account.</h4>
<hr />
#Html.ValidationSummary("", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(m => m.CompanyName, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.CompanyName, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.UserName, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.UserName, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.Email, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.Email, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#foreach (var la in Model.Languages)
{
<input type="checkbox"
name="SelectedLanguages" value="#la.Id" id="#la.Id" />
<label for="#la">#la.Title</label>
}
</div>
<div class="form-group">
#foreach (var bu in Model.BusinessUnits)
{
<input type="checkbox"
name="SelectedBusinessUnits" value="#bu.Id" id="#bu.Id" />
<label for="#bu.Id">#bu.Title</label>
}
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Register" />
</div>
</div>
}
I think you have to go the way of writing a custom validation routine accompanied with a ValidationAttribute. Don't think a simple "out-of-the-box" validator exists for checking if one or more values are present in an array.
Check out this SO post to point you in the right direction.
Basic setup:
public class ArrayContainsValueAttribute: ValidationAttribute
{
// your checks here (pseudo)
if(!array.Any())
return false;
return true;
}
[ArrayContainsValue(ErrorMessage = "Please select at least one business unit")]
public int[] SelectedBusinessUnits { get; set; }

Database First EF6 add data to table with Custom ViewModel

I using MVC 5.1 and EF 6.1 database first model. I don't like the view that was created by the scaffolding as it uses all the database columns. On the create view I just want the user to input the required fields all other data can be added through editing the details view. I have created the a view model to use other than the .edmx generated one. The view works as expected but when the create action button is click the post operation dies with a "[NullReferenceException: Object reference not set to an instance of an object.]
Here is my view model
public class DesktopCreateViewModel
{
[Required]
[Display(Name = "Serial Number")]
[StringLength(30)]
public string SERIAL_NUMBER { get; set; }
public int Model_DesktopId { get; set; }
public int DeviceType_DesktopId { get; set; }
[Required]
[StringLength(50)]
public string MAC1 { get; set; }
[Display(Name = "Arrival Date")]
public DateTime ARRIVAL_DATE { get; set; }
[Timestamp]
[Display(Name = "Record Updated")]
public DateTime RECORD_UPDATED { get; set; }
public int LocationId { get; set; }
public int Location_CodeId { get; set; }
public int MemoryId { get; set; }
public int MonitorId { get; set; }
public int PlantId { get; set; }
public int DepartmentId { get; set; }
public int Operating_SystemId { get; set; }
[ForeignKey("DeparmentId")]
public virtual Department Department { get; set; }
[ForeignKey("DeviceType_DesktopId")]
public virtual DeviceType_Desktop DeviceType_Desktop { get; set; }
[ForeignKey("Location_CodeId")]
public virtual Location_Code Location_Code { get; set; }
[ForeignKey("LocationId")]
public virtual Location Location { get; set; }
[ForeignKey("MemoryId")]
public virtual Memory Memory { get; set; }
[ForeignKey("Model_DesktopId")]
public virtual Model_Desktop Model_Desktop { get; set; }
[ForeignKey("MonitorId")]
public virtual Monitor Monitor { get; set; }
[ForeignKey("Operating_SystemId")]
public virtual Operating_System Operating_System { get; set; }
[ForeignKey("PlantId")]
public virtual Plant Plant { get; set; }
public IEnumerable<DesktopCreateViewModel> Create_Desktop { get; set; }
public virtual ICollection<DesktopCreateViewModel> Desktops { get; set; }
Here is the portion of the controller that fails
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ICollection<DesktopCreateViewModel> desktop)
{
if (ModelState.IsValid)
{
foreach (var item in desktop)
{
db.Entry(desktop).State = EntityState.Added;
}
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "DEPARTMENT1", desktop.DepartmentId);
ViewBag.DeviceType_DesktopId = new SelectList(db.DeviceType_Desktop, "Id", "DEVICE_TYPE", desktop.DeviceType_DesktopId);
ViewBag.Location_CodeId = new SelectList(db.Location_Code, "Id", "LOCATION_CODE1", desktop.Location_CodeId);
ViewBag.LocationId = new SelectList(db.Locations, "Id", "LOCATION1", desktop.LocationId);
ViewBag.MemoryId = new SelectList(db.Memories, "Id", "MEMORY1", desktop.MemoryId);
ViewBag.Model_DesktopId = new SelectList(db.Model_Desktop, "Id", "MODEL", desktop.Model_DesktopId);
ViewBag.MonitorId = new SelectList(db.Monitors, "Id", "MONITOR1", desktop.MonitorId);
ViewBag.Operating_SystemId = new SelectList(db.Operating_System, "Id", "OS", desktop.Operating_SystemId);
ViewBag.PlantId = new SelectList(db.Plants, "Id", "PLANT1", desktop.PlantId);
return View(desktop);
}
This is the View
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Desktop</h4>
<hr />
#Html.ValidationSummary(true)
<div class="form-group">
#Html.LabelFor(model => model.SERIAL_NUMBER, new { #class = "control-label col-md-4" })
<div class="col-md-8">
#Html.EditorFor(model => model.SERIAL_NUMBER)
#Html.ValidationMessageFor(model => model.SERIAL_NUMBER)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Model_DesktopId, "Model", new { #class = "control-label col-md-4" })
<div class="col-md-8">
#Html.DropDownList("Model_DesktopId", String.Empty)
#Html.ValidationMessageFor(model => model.Model_DesktopId)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.DeviceType_DesktopId, "Device Type", new { #class = "control-label col-md-4" })
<div class="col-md-8">
#Html.DropDownList("DeviceType_DesktopId", String.Empty)
#Html.ValidationMessageFor(model => model.DeviceType_DesktopId)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.MAC1, new { #class = "control-label col-md-4" })
<div class="col-md-8">
#Html.EditorFor(model => model.MAC1)
#Html.ValidationMessageFor(model => model.MAC1)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ARRIVAL_DATE, new { #class = "control-label col-md-4" })
<div class="col-md-8">
#Html.EditorFor(model => model.ARRIVAL_DATE)
#Html.ValidationMessageFor(model => model.ARRIVAL_DATE)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.RECORD_UPDATED, new { #class = "control-label col-md-4" })
<div class="col-md-8">
#Html.EditorFor(model => model.RECORD_UPDATED)
#Html.ValidationMessageFor(model => model.RECORD_UPDATED)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.LocationId, "Location", new { #class = "control-label col-md-4" })
<div class="col-md-8">
#Html.DropDownList("LocationId", String.Empty)
#Html.ValidationMessageFor(model => model.LocationId)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Location_CodeId, "Location Code", new { #class = "control-label col-md-4" })
<div class="col-md-8">
#Html.DropDownList("Location_CodeId", String.Empty)
#Html.ValidationMessageFor(model => model.Location_CodeId)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.MemoryId, "Memory", new { #class = "control-label col-md-4" })
<div class="col-md-8">
#Html.DropDownList("MemoryId", String.Empty)
#Html.ValidationMessageFor(model => model.MemoryId)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.MonitorId, "Monitor", new { #class = "control-label col-md-4" })
<div class="col-md-8">
#Html.DropDownList("MonitorId", String.Empty)
#Html.ValidationMessageFor(model => model.MonitorId)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.PlantId, "Plant", new { #class = "control-label col-md-4" })
<div class="col-md-8">
#Html.DropDownList("PlantId", String.Empty)
#Html.ValidationMessageFor(model => model.PlantId)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.DepartmentId, "Department", new { #class = "control-label col-md-4" })
<div class="col-md-8">
#Html.DropDownList("DepartmentId", String.Empty)
#Html.ValidationMessageFor(model => model.DepartmentId)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Operating_SystemId, "Operating System", new { #class = "control-label col-md-4" })
<div class="col-md-8">
#Html.DropDownList("Operating_SystemId", String.Empty)
#Html.ValidationMessageFor(model => model.Operating_SystemId)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-8 col-md-4">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
The action is expecting a collection of viewmodels (ICollection<DesktopCreateViewModel>), but the view only contains one. Try changing to:
public ActionResult Create(DesktopCreateViewModel desktop)

Resources