从视图传递下拉列表值到控制器

时间:2018-10-05 11:16:33

标签: c# .net

我需要将从视图的下拉列表中选择的值传递给控制器,到目前为止,我失败了。

我应该怎么做?

以下是我的“视图”代码:

@model NurseOneStop.SC.NurseProfile

@{
ViewBag.Title = "Settings";
}

<h2>Settings</h2>


 <div class="col-lg-8 col-md-6 profile_detail">
<h3>@Model.Title @Model.FirstName</h3>
<p><span>Profession:</span> @Model.Profession</p>
<p><span>Contact No:</span> <b>@Model.PhoneNumber </b>
<p><span>Email ID:</span> <b>@Model.EmailId </b>
    <br />
    <br />
    <br />
    <br />

<div>


    <div>
        <h5>Visibility Settings for Profile</h5>

        @Html.DropDownList("Profile_Settings", new List<SelectListItem>
            {
                 new SelectListItem{ Text="Public", Value = "1" },
                 new SelectListItem{ Text="Friends Only", Value = "2" },
                 new SelectListItem{ Text="Private", Value = "3" }
        }, "Select Visibility Type")


    </div>

    <button class="profile_btn">@Html.ActionLink("Save Settings", "UpdateSettings")</a></button>

</div>
</div>

下面是我的控制器:

public ActionResult UpdateSettings()
    {
        NurseProfile objNurseProfile = new NurseProfile();
        Int64 NurseId = ApplicationSession.CurrentUser.NurseId;
        if (NurseId != 0)
        {
            //objNurseProfile = objNurseDAL.UpdateProfileVisibility(NurseId, ProfileVisibility);
        }

        return View(objNurseProfile);
    }

2 个答案:

答案 0 :(得分:0)

您可以使用剃刀填充这样的下拉列表,然后通过模型(如MVC指令)将其发送。

@{
   List<SelectListItem> listItems= new List<SelectListItem>();
   listItems.Add(new SelectListItem
        {
          Text = "Public",
          Value = "1"
        });
   listItems.Add(new SelectListItem
        {
            Text = "Friends Only",
            Value = "2"
        });
   listItems.Add(new SelectListItem
        {
            Text = "Private",
            Value = "3"
        });
}

@Html.DropDownListFor(model => model.profileSettings, listItems, "Select Option")

@Html.ActionLink(
    "Save Settings",                                      // linkText
    "UpdateSettings",                                     // actionName
    "Settings",                                           // controllerName
    new {                                                 // routeValues
        SomeModel = Model
    },
    null                                                  // htmlAttributes
)

您应该像这样在您的方法中添加模型:

public ActionResult UpdateSettings(SomeModel model)
    {
        int setting = model.profileSettings; //your choice
        return View(objNurseProfile);
    }

并创建一个像这样的模型:

public class SomeModel
{
    [Required]
    public int profileSettings { get; set; }
}

答案 1 :(得分:0)

尝试以下代码:

[HttpGet]
    public ActionResult Settings(Int64? id, string returnUrl)
    {
        List<Keyword> objKeywordList = new List<Keyword>();
        List<SelectListItem> ProfileVisibility = new List<SelectListItem>();
        NurseProfileVisibility objNurseProfileVisibility = new NurseProfileVisibility();
        Int64 NurseId = ApplicationSession.CurrentUser.NurseId;
        objNurseProfileVisibility.NurseId = NurseId;

        SelectListItem objSelect = new SelectListItem { Text = "Profile Visibility", Value = "", Selected = true };
        objKeywordList = objKeywordDAL.GetKeywordsByType("ProfileVisibility").Results;

        var visibilityOption = (from kl in objKeywordList
                                select new SelectListItem
                                {
                                    Text = kl.KeywordText,
                                    Value = kl.KeywordValue.ToString(),
                                    Selected = false
                                }).ToList();

        if (id != 0)
        {
            Result res = objNurseDAL.GetProfileVisibilityById(NurseId);
            if (res.Status)
            {
                if(res.Results != null) {
                    objNurseProfileVisibility = res.Results;
                    if (objNurseProfileVisibility.NurseId != NurseId)
                    {
                        visibilityOption.FirstOrDefault(x => x.Value == objNurseProfileVisibility.ProfileVisibilityId.ToString()).Selected = true;
                    }
                }
            }
        }
        ViewBag.VisibilityOptions = visibilityOption;
        ViewBag.returnUrl = returnUrl;
        return View(objNurseProfileVisibility);
    }

您需要将值存储到数据库中,并使用列表和DAL从那里解析它们。