0

I'm quite new to ASP.Net and I'm trying to make a reservation to a specified branch. When I click the reservation link of a branch, it comes to a drop-down list for me to choose a branch from those already existed in the database. What I want is the branch has already been set based on the reservation link of that branch I clicked. In the database, the reservation entity has an attribute referencing to the branchId in the branch entity. I have viewed some related questions such as MVC5 - How to set “selectedValue” in DropDownListFor Html helper, but neither solved my question properly. I still have no idea of this for now.

The following images are what it looks like now. list of branches that can be reserved Create View for now

This is the create action in my reservation controller:

// GET: Reservations/Create
public ActionResult Create()
{
    ViewBag.branchId = new SelectList(db.Branches, "branchId", "name");
    return View();
}


[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "reservationId,branchId,customerId,date")] Reservation reservation)
    {
        reservation.customerId = User.Identity.GetUserId();
        ModelState.Clear();
        TryValidateModel(reservation);
        if (ModelState.IsValid)
        {
            db.Reservations.Add(reservation);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.branchId = new SelectList(db.Branches, "branchId", "name", reservation.branchId);
        return View(reservation);
    }

This is my reservation create view:

@model Mel_Medicare_Location_Reservation_System.Models.Reservation

@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Create</h2>


@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Reservation</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group ">
        @Html.LabelFor(model => model.branchId, "branchId", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">

            @Html.DropDownList("branchId", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.branchId, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.customerId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.customerId, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.customerId, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.date, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.date, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.date, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Alex Wang
  • 411
  • 3
  • 16

1 Answers1

0

You should have the dropdown options as a get-only property of your model, then use the @Html.DropDownListFor() HTML helper to bind model.branchId to that list, which will set the value appropriately.

I would re-write your Create() controller method to return the view with an instance of the Reservation view model bound to it. This will involve adding a constructor to the view model class which accepts a branch id as a parameter. Something like:

// GET: Reservations/Create
public ActionResult Create(int branchId)
{
    return View(new Reservation(branchId));
}

And in your Reservation model:

public class Reservation
{
    public int BranchId { get; set; }
    // Other properties...

    public SelectList BranchOptions
    {
        get
        {
            return new SelectList(db.Branches);
        }
    }

    public Reservation(int branchId)
    {
        this.BranchId = branchId;
    }
}

Then in your view:

@Html.DropDownListFor(m => m.BranchId, Model.BranchOptions, htmlAttributes: new { @class = "form-control" })
akerra
  • 1,017
  • 8
  • 18