Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC - model with collection, Submit

I have a Model that looks like

public class Patient
{
    private ICollection<Refill> _refills = new List<Refill>();
    public int Id { get; set; }
    public string FirstName { get; set; }
                  
    public virtual ICollection<Refill> Refills
    {
        get { return _refills; }
        set { _refills = value; }
    }
}

public class Refill
{
    public int Id { get; set; }

    public RefillActivityStatus RefillActivityStatus { get; set; }
    public DateTime? RefillDate { get; set; }
    public RefillType RefillType { get; set; }
    public string RXNumber { get; set; }
}

Here is what I am trying to do. I would like to Populate these in the View and when the user clicks save changes. I would like Entity Framework to save. The problem I am having is in the View I do this

@foreach (var m in Model.Refills)
{
     @Html.HiddenFor(model=>m.Id)
     <div class="editor-label">
         @Html.LabelFor(model => m.RXNumber)
     </div>
     <div class="editor-field">
         @Html.EditorFor(model => m.RXNumber)                                  
     </div>     
}

I am trying to do in the Controller something like this

[HttpPost]
public ActionResult Details(Patient patient)
{
    Patient p = db.Patients.Find(patient.Id);
    db.Entry(p).State = EntityState.Modified;
    db.Entry(p).CurrentValues.SetValues(patient);
    // Would include Refill changes too but it doesn't
    db.SaveChanges();

    return View(p);
}

However, Refills doesn't propagate on HttpPost. It is just empty, what do I need to do to fix it?

like image 963
Mike Diaz Avatar asked Jan 28 '13 13:01

Mike Diaz


People also ask

What is @model in Cshtml?

The @model directive allows access to the list of movies that the controller passed to the view by using a Model object that's strongly typed. For example, in the Index.cshtml view, the code loops through the movies with a foreach statement over the strongly typed Model object: CSHTML Copy.

Why TempData is used in MVC?

What is TempData and How to Use in MVC? TempData is used to transfer data from the view to the controller, the controller to the view, or from an action method to another action method of the same or a different controller. TempData temporarily saves data and deletes it automatically after a value is recovered.

How pass data from controller model in MVC?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.


1 Answers

Try this insted of your foreach:

@for (int i = 0; i < Model.Refills.Count(); i++)
{
    @Html.Hidden("Refills[" + i + "].Id", Model.Refills[i].Id)
    <div class="editor-label">
        @Html.Label("Refills[" + i + "].RXNumber", Model.Refills[i].RXNumber)
    </div>
    <div class="editor-field">
        @Html.Editor("Refills[" + i + "].RXNumber")
    </div>     
}
like image 181
lante Avatar answered Oct 17 '22 15:10

lante