85239

asp.net mvc3 UpdateModel exclude properties is not working

I have a class, which has 8 props / 8 columns in DB. But on a Edit page, i dont want to show the AddedDate or UserID field, since i dont want user to change it.

public class Voucher { public int ID { get; set; } public string Title { get; set; } public string SiteName { get; set; } public string DealURL { get; set; } public DateTime AddedDate { get; set; } public DateTime? ExpirationDate { get; set; } public string VoucherFileURL { get; set; } public Guid UserID { get; set; } }

Here is what I have for Edit controller:

// POST: /Voucher/Edit/5 [HttpPost] public ActionResult Edit(Voucher voucher) { if (ModelState.IsValid) { string[] excludeProperties = { "AddedDate", "UserID" }; UpdateModel(ModelState, "", null, excludeProperties); db.Entry(voucher).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(voucher); }

On Edit page, once i click on submit, i got the following error: System.Data.SqlServerCe.SqlCeException: An overflow occurred while converting to datetime.

Seems like the AddedDate didn't get excluded from the view model and triggered the error.

Would you please let me know how to fix it? Thanks!

public ActionResult Edit([Bind(Exclude = "AddedDate")]Voucher voucher)

no luck either

Answer1:

You are still passing in Voucher which could contain that field in it. I'm not sure what you are trying to accomplish with the UpdateModel here if you are already passing in a Voucher object? Pass in Voucher, set it to modified and save it. If you want to use whats in the database then you'll have to

<ol> <li>Load the object from the database</li> <li>UpdateModel and exclude the properties</li> <li>Save your entity.</li> </ol>

You could simply use a View Model and post that.


public class Voucher
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string SiteName { get; set; }
    public string DealURL { get; set; }
    public DateTime? ExpirationDate { get; set; }
    public string VoucherFileURL { get; set; }
    public Guid UserID { get; set; }
}

</pre>

and then load up your object from the db":


var voucher = db.Vouchers.Where(o=>o.ID==voucherViewModel.Id);
//manually copy the fields here then save it
//copy
db.SaveChanges();

</pre>
    

Recommend

  • mysql update qty on complete order array [duplicate]
  • Dynamics CRM 2011 - How to relate two different entities
  • Laravel parent/children relationship on it's own model
  • AWS S3 “404 Not Found”
  • Web Api 2: [Required] for value types?
  • The argument types 'Edm.String' and 'Edm.Int32' are incompatible for this operat
  • Web.Api not deserializing properties of Child Array in Post
  • MVC 4 Passing a list from a view to a controller
  • DateTime required in ModelState, however never set to be
  • Android: How to make my retrieved (from mysql) JSON parsed data added to ListView refresh it self ev
  • In MVC/Razor, how can you open a new view instead of as a partial view?
  • kendoui grid in mvc3 security vulnerability, how do i get around it?
  • “Value cannot be null or empty. Parameter name: contentPath” on a most unexpected line on postback w
  • File upload .NET Core 'IFormFile' does not contain a definition for 'SaveAsASync'
  • Reload MVC2 user control with jQuery
  • How does ASP.NET get line numbers in it's generic error handler
  • The ViewData item that has the key 'MaritalStatus' is of type 'System.String' bu
  • ViewModel properties stay null on HttpPost
  • Cant Run Default ASP.NET Web App In Visual Studio Code for Mac
  • My “Create” View in my “Index” View, is it possible?
  • Null Pointer Exception: HTTP Entity
  • Android json parse not working
  • The model item passed into the dictionary is of type A, but this dictionary requires a model item of
  • Symfony 2 form in embedded controller and AJAX
  • Render a treeview using a for-loop
  • Why action not found in my controller?
  • Returning Json from controller, never a success
  • how can resolve dodgy:unchecked/unconfirmed cast in sonar?
  • how to update a column in a table with unique values, but not all the rows, not incremented [duplica
  • Zend Framework 2, Module Redirect
  • IDX10503: Signature validation failed
  • stop execution in Custom validator if it false