【问题标题】:How to set a default value rather than choose from drop-down list in Create-View [duplicate]如何设置默认值而不是从 Create-View 的下拉列表中选择 [重复]
【发布时间】:2018-09-21 20:19:27
【问题描述】:

我对 ASP.Net 很陌生,我正在尝试预订指定的分支。当我点击一个分支的预订链接时,它会出现一个下拉列表,让我从数据库中已经存在的分支中选择一个分支。我想要的是已经根据我点击的那个分支的预订链接设置了这个分支。在数据库中,预订实体具有引用分支实体中的 branchId 的属性。我查看了一些相关问题,例如MVC5 - How to set “selectedValue” in DropDownListFor Html helper,但都没有正确解决我的问题。我暂时还没有这个概念。

以下图片是现在的样子。

这是我的预订控制器中的创建操作:

// 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);
    }

这是我的预订创建视图:

@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")
}

【问题讨论】:

    标签: c# asp.net-mvc razor


    【解决方案1】:

    您应该将下拉选项作为模型的 get-only 属性,然后使用@Html.DropDownListFor() HTML 帮助程序将 model.branchId 绑定到该列表,这将适当地设置值。

    我会重写您的Create() 控制器方法以返回带有绑定到它的 Reservation 视图模型实例的视图。这将涉及向视图模型类添加一个构造函数,该类接受分支 id 作为参数。比如:

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

    在您的预订模型中:

    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;
        }
    }
    

    那么在你看来:

    @Html.DropDownListFor(m => m.BranchId, Model.BranchOptions, htmlAttributes: new { @class = "form-control" })
    

    【讨论】:

      猜你喜欢
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多