Click here to Skip to main content
15,882,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an EF6 MVC application which I intend to do update operations but I am having issues on
1) how to edit Dropdown values
2) how to edit Date values
3)Assign default values for edit properties

I am able to create an AssetRequest entry as below :
C#
[HttpGet]
        public ActionResult Create()
        {
            var model = new AssetRequestViewModel();           
            service.GetEmpStat(model);          
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(AssetRequestViewModel model)
        {
            if (!ModelState.IsValid)
            {               
                service.GetEmpStat(model);              
            }
                   
            if (ModelState.IsValid)
            {             
                var model2 = new AssetRequest();
                model.UpdateModel(model2);               
                service.AddAssetRequest(model2 );                          
                return RedirectToAction("Index");               
            }
            return View(model);
        }

from AssetRequest.cs :
C#
public partial class AssetRequest
    {
       ...
       public int AssetRequestId { get; set; }
       public Nullable<int> EmployeeStatusId { get; set; }
       public Nullable<System.DateTime>
       public string SnrMgrSign { get; set; }
       public virtual EmployeeStatus EmployeeStatus { get; set; }
   }

and to ensure dropdowns bind correctly I used a ViewModel https://stackoverflow.com/questions/61882509/how-do-i-bind-a-dropdownlist-using-entity-framework-6-database-first-approach/61949197#61949197:
C#
public class AssetRequestViewModel
    {
        public AssetRequestViewModel(AssetRequest model)
        {          
            EmployeeStatusId = model.EmployeeStatusId;           
            SignatureDate = model.SignatureDate;
            SnrMgrSign = model.SnrMgrSign ;         
        }

        public AssetRequestViewModel()
        {
        }
             
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        [DataType(DataType.Date)]
        public DateTime? SignatureDate { get; set; }     
        public int? EmployeeStatusId { get; set; }
        public string SnrMgrSign  { get; set; }    
        public IEnumerable<SelectListItem> EmployeeStatuses { get; internal set; }
        
        public void UpdateModel(AssetRequest server)
        {                  
            server.EmployeeStatusId = EmployeeStatusId;
            server.SnrMgrSign  = SnrMgrSign;
            server.SignatureDate = SignatureDate;
        }
    }

and in the razor page I have :
Razor
@model FBChecklist.ViewModels.AssetRequestViewModel
...
..                                                            
<div class="col-sm-12 control-label no-padding-right" for="form-field-username">
                                                                @Html.DropDownListFor(m => m.EmployeeStatusId, Model.EmployeeStatuses, "", htmlAttributes: new { @class = "form-control" })
                                                                @Html.ValidationMessageFor(model => model.EmployeeStatusId, "", new { @class = "text-danger" })
                                                            </div>

                                                            <div class="col-sm-8">
                                                                @Html.EditorFor(m => m.SignatureDate, new { htmlAttributes = new { @class = "input-medium date-picker" } })

                                                                @Html.ValidationMessageFor(model => model.SignatureDate, "", new { @class = "text-danger" })
                                                            </div>


Now to do Update operations I use the actions below but Im not sure how I can update DropDowns since this method uses the model and not the ViewModel :
C#
public ActionResult Edit(int? id)
       {
           if (id == null)
           {
               return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
           }
           AssetRequest assetRequest = db.AssetRequest.Find(id);
           if (assetRequest == null)
           {
               return HttpNotFound();
           }
           return View(assetRequest);
       }


C#
[HttpPost, ActionName("Edit")]
     [ValidateAntiForgeryToken]
     public ActionResult EditAssetRequest(int? id)
     {
         if (id == null)
         {
             return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
         }
         var requestToUpdate = db.AssetRequest.Find(id);
         if (TryUpdateModel(requestToUpdate, "",
             new string[] { "SignatureDate", "EmployeeStatusId", "SnrMgrSign"}))
         {
             try
             {
                 db.SaveChanges();
                 return RedirectToAction("Index");
             }
             catch (RetryLimitExceededException /* dex */)
             {
                 ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
             }
         }
         return View(requestToUpdate);
     }


How can I edit these values ?

What I have tried:

I have tried to add SelectListItem variables to the Model itself but I'm getting a lot of errors and this also appears to be unnecessary duplication of code - They should be a way of updating these values .. even when using ViewModels
Posted
Updated 22-Nov-21 23:17pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900