Asp.net mvc3 UpdateModel exclude properties is not working?

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.

Up vote 1 down vote favorite share g+ share fb share tw.

I have a class, which has 8 props / 8 columns in DB. But on a Edit page, I don't want to show the AddedDate or UserID field, since I don't 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 asp. Net-mvc-3 link|improve this question edited Mar 17 at 5:16 asked Mar 17 at 4:10Shadow_boi405111 73% accept rate.

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 Load the object from the database UpdateModel and exclude the properties Save your entity. 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; } } 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().

I think I misunderstood sth. I thought the Voucher object is the same as the ModelState, they both have 8 properties. So I wanted to exclude 2 properties from the ModelState when the Voucher is being saved.

– Shadow_boi Mar 17 at 4:47 No modelstate is the state if the validation of your model. By the time you receive voucher MVC has validated it and out the results of the validation into modelstate. You wouldn't pass that back into updatemodel.

UpdateModel is used when you want to 'update some model object' with the posted or URL values . In this case you already have been given that object named voucher with these properties in it. – Adam Tuliper Mar 17 at 4:53 When User edit Voucher, I don't allow them to see or change the addedDate or UserID.

So Voucher Object got passed into the Edit controller will surely contain no value for addedDate or UserID. So when the voucher object is saved to db, it triggers error. Any solution for it?

Hope you understand. Thanks. – Shadow_boi Mar 17 at 5:05 tried the following, but no luck: public ActionResult Edit(Bind(Exclude = "AddedDate")Voucher voucher) – Shadow_boi Mar 17 at 5:15 Then create a new model object called VoucherEditViewModel.

Leave off the fields you don't want them to see. YEs, you are copying fields from your Voucher object into this object. This is no 'code duplication' as they serve different purposes.

This ViewModel is specific to your screen. The Voucher object is your domain object. Then load the Voucher object from your db, copy the values you want to it, and save it.

An alternative is to use UpdateModel(yourVoucherLoadedFromDb) but be aware you could in theory be open to the end user posting values they don't – Adam Tuliper Mar 17 at 19:41.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions