I need to Pass a Value selected from drop down in a View to a Controller and as of now I am failing miserably.
How should I do that?
Following is my code for the View:
@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>
Below is my Controller for the same:
public ActionResult UpdateSettings()
{
NurseProfile objNurseProfile = new NurseProfile();
Int64 NurseId = ApplicationSession.CurrentUser.NurseId;
if (NurseId != 0)
{
//objNurseProfile = objNurseDAL.UpdateProfileVisibility(NurseId, ProfileVisibility);
}
return View(objNurseProfile);
}
You can use razor to fill a dropdownlist like this and then send it via the Model (like MVC dictates).
@{
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
)
And you should have your model in your method like so:
public ActionResult UpdateSettings(SomeModel model)
{
int setting = model.profileSettings; //your choice
return View(objNurseProfile);
}
And create a model like so:
public class SomeModel
{
[Required]
public int profileSettings { get; set; }
}
Try it with the following code:
[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);
}
You need to store the values into the Database and parse them from there using List and DAL's.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.