Home > Software design >  ASP.NET How to pass data from controller to view and back to controller
ASP.NET How to pass data from controller to view and back to controller

Time:07-15

Im trying to sending data from controller to view and then Im trying to get data back to another action.

In my case I am trying to send a View with Model and datay(Date,Id,Room) from the controller. In the view I want to add the Name value to the submitted model and then send the whole model back to the next controller action. The problem is that in the next action only the Name value is shown and the other values are null. Does anyone know what to do?

Model im using.

public class ReservationModel
    {
        [Key]
        public int Id { get; set; }

        public string Name { get; set; }

        public DateTime Cas { get; set; }

        public RoomModel Room { get; set; }
    }

Action where from Im displaying the View with data Cas, Id and Room

 [HttpGet]
    [Route("Home/Room/Create_reservation")]
    public async Task<IActionResult> CreateReservation(DateTime date)
    {
        ReservationModel reservation = await _reservation.PrepareModel(date, _id);
        return View(reservation);
    }

The view where I wanna add Name value and send it whole back.

@model ReservationModel


<form asp- asp-controller="Home" asp-action="CreateReservation" method="post">
    <input required="required" type="text"  placeholder="Name"
           asp-for="Name" />
    <span asp-validation-for="Name" ></span>
    <input type="submit" 
           style="width:auto;" value="Add" />

</form>

Action where in debuging show that reservationModel has only Name data other is null.

 [HttpPost]
    [Route("Home/Room/Create_reservation")]
    public IActionResult CreateReservation([Bind("Name","Room","Cas","Id")] ReservationModel reservationModel)
    {

        return View();
    }

CodePudding user response:

If you want to transfer every field in the model then you'd need inputs for every field in the model. Currently you only have one input for Name:

<input required="required" type="text"  placeholder="Name" asp-for="Name" />

Since you're only sending the Name value, then only the Name value is sent.

Based on a comment above:

Im trying just to add Name value to data I have send from CreateReservation and in Post Action just save whole model to Database.

Then there are two pieces of data you want, the Name and the Id. Include the Id as another input:

<input type="hidden" asp-for="Id" />

Then in the resulting controller action you'll have both the Name and the Id. Arguably you should create a custom view model for this and not use the same model, just to avoid future confution. But for the sake of brevity here the same model will work. It will just only have those two properties populated, since they're the only two you're populating.

In that controller action you would use that Id value to fetch the entire model from the data again. Then you'd update that model instance with the Name which was provided by the input, and re-save that fetched model instance.

Making some assumptions, but just to illustrate:

[HttpPost]
[Route("Home/Room/Create_reservation")]
public IActionResult CreateReservation([Bind("Name","Id")] ReservationModel reservationModel)
{
    // fetch from the db
    var existingModel = _context.Reservations.Single(r => r.Id == reservationModel.Id);

    // update the value
    existingModel.Name = reservationModel.Name;

    // save
    _context.SaveChanges();

    // as an aside, this should probably be a redirect and not a view
    return View();
}

After all, why send all that data to the client and then back to the server if you're not expecting the client to do anything with it, or even see it? Even populating hidden form fields, you're still giving the client the ability to edit all of the other data. (Which means it's also worth noting here that the client can edit the id value, so you'll want to perform some kind of authorization to make sure they're not editing an object they're not allowed to edit.)

  • Related