Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在asp.net mvc中,如何在不使用模型类的情况下将数据从view dropdownlist获取到controller_C#_Jquery_Asp.net Mvc - Fatal编程技术网

C# 在asp.net mvc中,如何在不使用模型类的情况下将数据从view dropdownlist获取到controller

C# 在asp.net mvc中,如何在不使用模型类的情况下将数据从view dropdownlist获取到controller,c#,jquery,asp.net-mvc,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");

控制器

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`
PublicActionResult Isearch(字符串办公室类型、字符串部门、字符串文件类别、字符串fromdate、字符串todate)
{
ViewBag.officetype=新建选择列表(entity.TBL_officetype.ToList(),“OFID”,“OFFICE_TYPE”);
//var list=newselectlist(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=新选择列表(entity.TBL_FILECATEGORY.ToList(),“FILECATEGORY”,“FILECATEGORY”);
ViewBag.filecategory=list4;
var myinboxdata=entity.TBL_INBOX.ToList();
//var myinboxs=来自entity.TBL\u收件箱中的res
//选择新{res.OFFICETYPE,res.DEPARTMENTCODE,res.DESCRIPTION};
var myinbox1=从entity.TBL_收件箱中的s选择s;
if(String.IsNullOrEmpty(fromdate)和&String.IsNullOrEmpty(todate))
{
//DateTime fdate=Convert.ToDateTime(fromdate).Date;
//DateTime tdate=Convert.ToDateTime(todate).Date;
myinbox1=实体.TBL_收件箱。其中(s=>s.OFFICETYPE.Contains(OFFICE_TYPE)和&s.DEPARTMENTCODE.Contains(DEPARTMENT)和&s.FILECATEGORY.Contains(FILECATEGORY));
}
其他的
{
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 x.OFFICETYPE.GroupBy(x=>new{x.OFFICETYPE,x.DEPARTMENTCODE,x.DESCRIPTION})。选择(g=>new=>{g.Key.OFFICETYPE,g.Key.DEPARTMENTCODE,g.Key.DESCRIPTION,Unread=g.Count()});
List mydata=new List();
foreach(数据中的var myinbox)
{
mydata.Add(new Models.Inbox{OType=myinbox.OFFICETYPE,DCode=myinbox.DEPARTMENTCODE,Desc=myinbox.DESCRIPTION,Unread=myinbox.Unread});
}
ViewBag.data=mydata.ToList();
返回视图(“收件箱”);
}`在这里输入代码`
在上面的代码中,我从视图中传递数据。在传递数据的同时,我得到office type和department的键。相反,我想要得到它们的值(文本)

看法

@使用(Html.BeginForm(“Isearch”、“Transactions”、FormMethod.Post))
{
我的收件箱
办公字体
@*@Html.DropDownList(“officetype”,ViewBag.officetype作为SelectList,新建{@class=“form control input sm”,@id=“name”})*@
@Html.DropDownList(“OFFICE_TYPE”,ViewBag.officetype作为SelectList,新建{@class=“form control input sm”,@id=“OFFICE”,name=“OFFICE”})
部门
@*@Html.DropDownList(ViewBag.Department,Enumerable.Empty(),新的{@class=“form control input sm”})*@,
文件类别
@Html.DropDownList(“FILECATEGORY”,ViewBag.FILECATEGORY作为SelectList,新建{@class=“form control input sm”,@id=“name”})
从日期开始
@TextBox(“fromdate”,null,new{@id=“fromdate”,@class=“form control input sm”})
@**@
迄今为止
@TextBox(“todate”,null,new{@id=“todate”,@class=“form control input sm”})
@**@

}
上面的代码是视图代码,其中我可以通过id和name属性传递值。 在上面的代码中,我从视图中传递数据。传递数据时,我得到的是office type和department的键。相反,我想得到它们的值(文本)

给您一个示例:

<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>
使用AJAX

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

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

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

            }
        });
控制器

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

        string gtGender = gender;

    }

使用
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();
}

为了更好地帮助您,您需要共享视图代码/JS代码,调用控制器查看如何传递参数
  [ScriptMethod(ResponseFormat = ResponseFormat.Json)]       
   public void GetDemoData(String gender)
    {

        string gtGender = gender;

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