如何在不使用asp.net mvc中的模型类的情况下从视图下拉列表中获取数据到控制器

时间:2018-05-03 16:11:45

标签: c# jquery asp.net-mvc

控制器

public ActionResult Isearch( string OFFICE_TYPE,string DEPARTMENT, string FILECATEGORY,string fromdate,string todate)
            {

                ViewBag.officetype = new SelectList(entity.TBL_OFFICETYPE.ToList(), "OFID", "OFFICE_TYPE");
                //var list = new SelectList(entity.TBL_OFFICETYPE.Select(r => r.OFFICE_TYPE).ToList());

                var list2 = new SelectList(entity.TBL_DEPARTMENT.Select(r => r.DEPARTMENTCODE).ToList());
                ViewBag.department = list2;
                var list4 = new SelectList(entity.TBL_FILECATEGORY.ToList(), "FILECATEGORY", "FILECATEGORY");
                ViewBag.filecategory = list4;


                var myinboxdata = entity.TBL_INBOX.ToList();
                //var myinboxs = from res in entity.TBL_INBOX
                //               select new { res.OFFICETYPE, res.DEPARTMENTCODE, res.DESCRIPTION };
                var myinbox1=from s in entity.TBL_INBOX select s;
                if (String.IsNullOrEmpty(fromdate)&& String.IsNullOrEmpty(todate))
                {
                    //DateTime fdate = Convert.ToDateTime(fromdate).Date;
                    //DateTime tdate = Convert.ToDateTime(todate).Date;
                    myinbox1 = entity.TBL_INBOX.Where(s => s.OFFICETYPE.Contains(OFFICE_TYPE) && s.DEPARTMENTCODE.Contains(DEPARTMENT) && s.FILECATEGORY.Contains(FILECATEGORY));

                }
                else
                {

                    myinbox1 = entity.TBL_INBOX.Where(s => s.OFFICETYPE.Contains(OFFICE_TYPE) && s.DEPARTMENTCODE.Contains(DEPARTMENT) && s.FILECATEGORY.Contains(FILECATEGORY) && (s.UDATE >= Convert.ToDateTime(fromdate).Date && s.UDATE <= Convert.ToDateTime(todate)));

                }
                var data = myinbox1.OrderBy(x => x.OFFICETYPE).GroupBy(x => new { x.OFFICETYPE, x.DEPARTMENTCODE, x.DESCRIPTION }).Select(g => new { g.Key.OFFICETYPE, g.Key.DEPARTMENTCODE, g.Key.DESCRIPTION, Unread = g.Count() });
                List<Inbox> mydata = new List<Models.Inbox>();
                foreach (var myinbox in data)
                {
                    mydata.Add(new Models.Inbox { OType = myinbox.OFFICETYPE, DCode = myinbox.DEPARTMENTCODE, Desc = myinbox.DESCRIPTION, Unread = myinbox.Unread });
                }
                ViewBag.data = mydata.ToList();
                return View("Inbox");
            }`enter code here`

在上面的代码中我从视图传递数据。在传递数据的同时,我获得了办公室类型和部门的关键。相反,我想得到他们的价值(文本)

查看

@using (Html.BeginForm("Isearch", "Transactions", FormMethod.Post))
    {
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">My InBox</h3>
            </div>
            <div class="panel-body">
                <div class="col-md-12 col-sm-12">
                    <div class="form-group col-md-4 col-sm-4">
                        <label for="Office">OfficeType</label>
                        @*@Html.DropDownList("officetype", ViewBag.officetype as SelectList, new { @class = "form-control input-sm", @id = "name" })*@
                        @Html.DropDownList("OFFICE_TYPE", ViewBag.officetype as SelectList, new { @class = "form-control input-sm", @id = "Office",name="Office" })

                    </div>
                    <div class="form-group col-md-4 col-sm-4" id="depart">
                        <label for="DEPARTMENT">Department</label>
                        @*@Html.DropDownList( ViewBag.Department,Enumerable.Empty<SelectListItem>(), new { @class = "form-control input-sm"})*@,
                        <select id="DEPARTMENT" name="DEPARTMENT" class = "form-control input-sm" ></select>
</div>
                    <div class="form-group col-md-4 col-sm-4">
                        <label for="FILECATEGORY">FILECATEGORY  </label>
                        @Html.DropDownList("FILECATEGORY", ViewBag.filecategory as SelectList, new { @class = "form-control input-sm", @id = "name" })
                    </div>
                    <div class="form-group col-md-4 col-sm-4">
                        <label>From Date</label>
                        @Html.TextBox("fromdate", null, new { @id = "fromdate", @class = "form-control input-sm" })
                        @*<input name="fdate" type="text" id="fromdate" class="form-control input-sm" />*@
                    </div>
                    <div class="form-group col-md-4 col-sm-4">
                        <label>To Date</label>
                        @Html.TextBox("todate", null, new { @id = "todate", @class = "form-control input-sm" })
                        @*<input type="text" id="todate" class="form-control input-sm" />*@
                    </div>
                    <div class="form-group col-md-4 col-sm-4">
                        <br />
                        <input type="submit" value="Search" class="btn btn-primary" />
                    </div>
                </div>
            </div>
        </div>
    }

上面的代码是视图代码,其中我通过id和name属性传递值。 在上面的代码我从视图传递数据。在传递数据的同时,我获得了办公室类型和部门的关键。相反,我想得到他们的价值(文本)

2 个答案:

答案 0 :(得分:1)

给你一个例子:

<select id="drpgenderid" name="drpgendername">


<option value="1">@Html.Label("FEMALE",Model.gender, new { @style = "", @id = "new_id_gender", @name = "name_new_gender", @placeholder = "Gender", @enabled = "false", @value = "FEMALE" })</option>

<option selected="selected" value="2">@Html.Label("MALE",Model.gender, new { @style = "", @id = "new_id_gender", @name = "name_new_gender", @placeholder = "Gender", @enabled = "false", @value = "MALE" })</option>

</select>

<强> SCRIPT

 function GetData()
        {

            var pdrp = document.getElementById("drpgenderid");
            gender = pdrp.options[pdrp.selectedIndex].value;

        }

使用 AJAX

       GetData();
        $.ajax({
            type: "post",
            contentType: "application/json; charset=utf-8",

            url: "@Url.Action("GetDemoData")",

            data: "{'gender':'" + gender.trim() + "'}",
            success: function (data) {

            }
        });

<强> CONTROLLER

  [ScriptMethod(ResponseFormat = ResponseFormat.Json)]       
   public void GetDemoData(String gender)
    {

        string gtGender = gender;

    }

答案 1 :(得分:0)

使用ViewBag@Html.DropDownList我们可以在不使用模型数据的情况下下拉

在控制器中

public ActionResult dropdown() {

    var customers = new List<Customer>();
    customers.Add(new Customer { Name = "Airi Satou", ID = 1 });
    customers.Add(new Customer { Name = "Brenden Wagner", ID = 2 });
    customers.Add(new Customer { Name = "Brielle Williamson", ID = 2 });
    ViewBag.DropData = customers;

    return View();
}

在视图中

 @Html.DropDownList("CustomerDropDown", new SelectList(ViewBag.DropData, "ID", "Name"), new { @class="form-control"})

 @Html.DropDownList("CustomerDropDown2", new SelectList(ViewBag.DropData, "Name", "Name"), new { @class="form-control"})