0

I have a MachineSettings entity and this entity has a Machine entity as a foreign key.

public class Machine
{
    [Key]
    public int MachineId { get; set; }

    [Required]
    [Display(Name = "Machine Type")]
    public string MachineName { get; set; }        
}

public class MachineSettings
{        
    [Key]
    public int MachineSettingId { get; set; }

    [Required]
    public string Password { get; set; }

    [Required]
    public int ReferenceKeyLength { get; set; }

    [Required]
    public virtual Machine MachineId { get; set; }
}

In my controller class, when I created an Edit method as POST:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(MachineSettings machineSettings)
{
        if (ModelState.IsValid)
        {
            db.Entry(objapp).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(machineSettings);
}

On the page I have shown the list of machines in drop down list, when do the changes and select another machine in dropdown and click on save. The Edit method is called with a MachineSetting object. Here my ModelState.IsValid always false.

On checking the object in debug mode, I am getting the MachineId from the drop down, but MachineName is not returned, so how to avoid MachineName checking in this so that ModelState.IsValid property will be true?

Serge
  • 40,935
  • 4
  • 18
  • 45
Shubhit304
  • 181
  • 1
  • 15

2 Answers2

3

try to fix the classes

public class MachineSettings
    {        
        [Key]
        public int MachineSettingId { get; set; }

        [Required]
        public string Password { get; set; }

        [Required]
        public int ReferenceKeyLength { get; set; }

        [Required]
        public int? MachineId { get; set; }
      
        public virtual Machine Machine { get; set; }
    }

public class Machine
    {
        [Key]
        public int MachineId { get; set; }

        [Required]
        [Display(Name = "Machine Type")]
        public string MachineName { get; set; } 

         public virtual ICollection<MachineSettings> MachineSettings { get; set; }       
    }

and create view model for the view

public class MachineViewModel
{
public MachineSettings  MachineSettings {get; set;}
public IEnumerable<SelectListItem> Machines { get; set; }

}

and action

public ActionResult Edit(MachineViewModel viewModel)
Serge
  • 40,935
  • 4
  • 18
  • 45
1

Maybe you should use a specific class for your ViewModel. ViewModels are for Views(UI) <--> Controller intercommunication, and usually are diferrent classes than the persistance (EF) layer. So your ViewModel doesn't even needs a full Machine property. Could be something like that:

public class MachineSettingsViewModel
{        
    public int MachineSettingId { get; set; } // Only if you are using the ViewModel for edit

    [Required]
    public string Password { get; set; }

    [Compare("Password")]
    public string ConfirmPassword { get; set; }

    [Required]
    public int ReferenceKeyLength { get; set; }

    [Required]
    public int MachineId { get; set; }
  
    public IEnumerable<SelectListItem> Machines { get; set; } // Collection with the options for the machine selector. Must be filled in the controller from query to service or DB
}

The ViewModel may include properties only used in the view, or needed for form validation but not needed in the DB.

In the controller "Get" action, you create and fill the viewmodel, then you pass it yo the view, and in the "Post" method you validate the viewmodel and convert it to a entity to be saved in the DB.

Take a look at What is ViewModel in MVC?. It's a more detailed explanation of ViewModels vs Models.

Pepelui360
  • 459
  • 2
  • 8