gpt4 book ai didi

c# - 如何在不使用asp.net mvc中的模型类的情况下从 View 下拉列表获取数据到 Controller

转载 作者:行者123 更新时间:2023-12-01 04:36:55 25 4
gpt4 key购买 nike

Controller

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`

在上面的代码中,我从 View 中传递数据。在传递数据时,我得到了办公室类型和部门的 key 。相反,我想获取它们的值(文本)

查看

@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>
}

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

最佳答案

举个例子:

<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>

脚本

 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;

}

关于c# - 如何在不使用asp.net mvc中的模型类的情况下从 View 下拉列表获取数据到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50159581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com