使用ViewBag从数据库中以HTML格式填充下拉列表

时间:2018-12-10 09:18:57

标签: razor html-select viewbag

我能够将值获取到ViewBag中,然后将这些值传递到HTML页面中。我面临的问题是,一旦将值传递到HTML中,就无法将值填充到下拉列表中。

以下是我的相同代码:

@using PagedList;
@using PagedList.Mvc;
@model IPagedList<NurseOneStop.SC.NurseProfile>

@{
ViewBag.Title = "People";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="peoplemain-container" ng-controller="connectCtrl">


<div class="row">
    <div class="search-box">
        @using (Html.BeginForm("People", "Nurse", FormMethod.Get))
        {

            <div class="j_searchName">
                <input class="form-control" name="FirstName" placeholder="Search by FirstName" value="@ViewBag.FilterNurseSearch.FirstName" />
                <input type="hidden" name="Connected" placeholder="Search Name" value="@(ViewBag.Connected == true ?"true":"false")" />

            </div>
            <div class="j_search2">
                <center><button type="submit" class="btn btn-primary sb">Search</button></center>
            </div>
        }
    </div>

    <div class="search-box">
        @using (Html.BeginForm("People", "Nurse", FormMethod.Get))
        {

            <div class="j_search">
                <input class="form-control" name="FirstName" placeholder="Search by FirstName" value="@ViewBag.FilterNurseSearch.FirstName" />
                <input type="hidden" name="Connected" placeholder="Search Name" value="@(ViewBag.Connected == true ?"true":"false")" />
            </div>

            <div class="j_search">
                <input class="form-control" name="LastName" placeholder="Search by LastName" value="@ViewBag.FilterNurseSearch.LastName" />
            </div>

            <div class="j_search">
                <input id="pinput" name="JobLocation" class="form-control" type="text" value="@ViewBag.FilterNurseSearch.JobLocation" placeholder="Enter a location">
                <input id="Latitude" name="Latitude" type="hidden" value="@ViewBag.FilterNurseSearch.Latitude">
                <input id="Longitude" name="Longitude" type="hidden" value="@ViewBag.FilterNurseSearch.Longitude">
            </div>


            <div class="j_search">
                <select class="form-control" name="Profession">
                    <option value="@ViewBag.Profession" selected>Select Profession</option>
                    @for (int i = 1; i < ViewBag.Profession.Count + 1; i++)
                    {
                        <option value=@i @(ViewBag.FilterNurseSearch.Profession == i ? "selected" : "")>@ViewBag.Profession[i].Text</option>
                    }
                </select>
            </div>


            <div class="form-group">
            @Html.LabelFor(model => model., new { @class = "control-label" })
            @Html.DropDownListFor(model => model.ParentProfessionId, (List<SelectListItem>)ViewBag.Profession, new { @class = "form-control" })
                @Html.DropDownListFor(model => model.ParentProfessionId, (List<SelectListItem>)TempData["TempModel"], new { @class = "form-control" })

            </div>

            <div class="j_search2">
                <center><button type="submit" class="btn btn-primary sb">Advance Search</button></center>
            </div>
        }
    </div>


</div>




@foreach (var item in Model)
{
    <div class="people_container">
        <div class="connect_profile"><img src="@(item.ProfileUrl== ""  ?"/image/placeholder.jpg" : @item.ProfileUrl)" title="@item.Title&nbsp;@item.FirstName&nbsp;@item.LastName" /></div>
        <div class="clear"></div>
        <div class="job_box">
            <p><b>@item.Title&nbsp;@item.FirstName&nbsp;@item.LastName</b></p>
            <p><span>@item.Profession</span></p>
        </div>
        <div class="job_box_btn">
            <button class="details_btn">
                @Html.ActionLink("View Nurse", "NurseView", "Nurse", new { NurseId = item.NurseId }, new { @class = "" })
            </button>

            <button class="details_btn">
                <a style="display:@(item.Status==0? "block": "none")" id="connect_@item.NurseId" NurseFriendId="@item.NurseFriendId" ng-click="InviteNurse(@item.NurseId, 1)">Connect</a>
                <a style="display:@(item.Status==1? "block": "none")" id="cancel_@item.NurseId" NurseFriendId="@item.NurseFriendId" ng-click="InviteNurse(@item.NurseId,3)">Cancel</a>
                <a style="display:@(item.Status==2 || item.Status==3? "block": "none")" id="message_@item.NurseId" NurseFriendId="@item.NurseFriendId" href="/Nurse/GetInbox?FromNurseId=@NurseOneStop.WebSite.Models.ApplicationSession.CurrentUser.NurseId&ToNurseId=@item.NurseId&FromRecruiterId=&ToRecruiterId=">Message</a>
                <div class="tick-right"><a style="display:@(item.Status==4? "block": "none")" id="accept_@item.NurseId" NurseFriendId="@item.NurseFriendId" ng-click="InviteNurse(@item.NurseId,2);"> <i class="fa fa-check"></i></a></div>
                <div class="tick-wrong"><a style="display:@(item.Status==4? "block": "none")" id="reject_@item.NurseId" NurseFriendId="@item.NurseFriendId" ng-click="InviteNurse(@item.NurseId,3);"><i class="fa fa-times"></i></a></div>

            </button>
        </div>
    </div>

}

<!--Confirm Modal -->
<div id="confirm" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content cancel-invitemodal">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Cancel the Invitation to Connect</h4>
            </div>
            <div class="modal-body cancel-invitetion">
                <h2>Are you sure you want to Cancel the Invitation to Connect</h2>
                <div class="cancel-box">
                    <input type="button" class="btn" ng-click="RequestCancel(SelectedNurseId,3)" value="CANCEL INVITE">
                    <input type="button" class="btn" ng-click="Cancel()" value="DO NOT CANCEL INVITE">
                </div>
            </div>
        </div>

    </div>
</div>

<div class="pagination">
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
    @Html.PagedListPager(Model, page => Url.Action("People", new { page, Connected = ViewBag.Connected, SearchName = ViewBag.SearchName }), PagedListRenderOptions.OnlyShowFivePagesAtATime)
</div>

</div>

@section scripts{

<script>
    function initMap() {
        var input = document.getElementById('pinput');
        var autocomplete = new google.maps.places.Autocomplete(input);
        autocomplete.addListener('place_changed', function () {
            var place = autocomplete.getPlace();
            console.log(JSON.stringify(place));
            if (!place.geometry) {
                window.alert("Autocomplete's returned place contains no geometry");
                return;
            }
            var latlong = JSON.parse(JSON.stringify(place.geometry.location));
            document.getElementById('Latitude').value = latlong.lat;
            document.getElementById('Longitude').value = latlong.lng;
        });
    }
    initMap();

    function showhide(id) {
        $("ul").hide();
        $("#newpost" + id).toggle();
    }

    $('#pinput').on('input', function () {
        document.getElementById('Latitude').value = '';
        document.getElementById('Longitude').value = '';
        // do your stuff
    });
</script>

}

以下是我使用控制器的方式:

public void DropDown()
    {
        ProfessionDAL objProfessionDAL = new ProfessionDAL();
        Profession objProfession = new Profession();
        List<Profession> objProfessionList = new List<Profession>();
        SelectListItem objSelect = new SelectListItem { Text = "Select Profession", Value = "", Selected = true };
        objProfessionList = objProfessionDAL.GetProfessionList().Results;
        var profession = (from kl in objProfessionList
                          select new SelectListItem
                          {
                              Text = kl.ProfessionName,
                              Value = kl.ProfessionId.ToString(),
                              Selected = false
                          }).ToList();
        profession.Insert(0, objSelect);
        ViewBag.Profession = profession;
    }


    public ActionResult People(bool Connected, int? page, FilterNurseSearch objFilterNurseSearch)
    {
        if (ApplicationSession.CurrentUser == null)
        {
            //redirect
            return RedirectToAction("Login", "Account");
        }
        DropDown();
        long NurseId = ApplicationSession.CurrentUser.NurseId;
        ViewBag.FilterNurseSearch = objFilterNurseSearch;
        Result objResult = objNurseDAL.GetNurses(NurseId, Connected, objFilterNurseSearch);

        int pageSize = 12;
        int pageNumber = (page ?? 1);
        ViewBag.Connected = Connected;
        return View(((List<NurseProfile>)objResult.Results).ToPagedList(pageNumber, pageSize));


    }

我知道我犯了一个愚蠢的错误,这是我无法捕捉的,因此,请其他开发人员来帮助我。

0 个答案:

没有答案