Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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 4_C#_Asp.net_Razor_Asp.net Mvc 4 - Fatal编程技术网

C# 尝试创建下拉列表和文本框,将列表项设置为数据,将文本框设置为值-asp.net mvc 4

C# 尝试创建下拉列表和文本框,将列表项设置为数据,将文本框设置为值-asp.net mvc 4,c#,asp.net,razor,asp.net-mvc-4,C#,Asp.net,Razor,Asp.net Mvc 4,我正在尝试创建一个基于服务器名或打印机名进行搜索的搜索表单。以下是我的控制器的代码段: List<SelectListItem> items = new List<SelectListItem>(); items.Add(new SelectListItem { Text = "Server Name", Value = "ServerName" }); items.Add(new SelectListItem { Text = "Prin

我正在尝试创建一个基于服务器名或打印机名进行搜索的搜索表单。以下是我的控制器的代码段:

List<SelectListItem> items = new List<SelectListItem>();
        items.Add(new SelectListItem { Text = "Server Name", Value = "ServerName" });
        items.Add(new SelectListItem { Text = "Printer Name", Value = "PrinterName" });

        ViewData["newlist"] = items;

我很确定我的控制器和视图都不正确。

不太喜欢这种方法。。。Http通过向控制器发送每个
输入的值来工作。根据控制器中接收到的值,您应该能够返回适当的页面和信息

因此,如果将下拉列表命名为searchType,将文本框命名为searchCriteria,则会有一个很好的查询字符串,如:
/Search?searchType=Printer&searchCriteria=epson
。在控制器中,您应该能够接收这些信息并返回相应的页面(无论是打印机还是服务器)

如果使用这种方法,您可以创建一个名为SearchType的
enum
,并使用它而不是
string
,这将允许您执行以下操作:
If(SearchType==SearchType.Printer)

如果您想使用您的方法,您可以在尝试搜索和附加URL时从输入中获取值:

@Html.DropDownList("searchType",ViewData["newlist"] as SelectList)
@Html.TextBox("searchCriteria")
<button type="button" onclick="GoToSearch();">Search</button>

function Search() {

    // assumption of jquery (use document.getElementById otherwise...)
    var type = $('#searchType').val();
    var searchCriteria = $('#searchCriteria').val();

    window.location = '@Url.Action("Search", "PrintQueues")' + '?' + type + '=' + searchCriteria;
}
@Html.DropDownList(“searchType”,ViewData[“newlist”]作为SelectList)
@Html.TextBox(“搜索条件”)
搜寻
函数搜索(){
//假设使用jquery(否则使用document.getElementById…)
var type=$('#searchType').val();
var searchCriteria=$('#searchCriteria').val();
window.location='@Url.Action(“搜索”,“打印队列”)'+'?'+类型+'='+搜索条件;
}

很乐意回答任何问题。

您确定不想让它显示
myservernaem
您有一个输入错误,为什么要将它作为查询字符串传递?在post Controllerb上使用formm集合不是更容易吗?因为这是一个简单的搜索,可以用查询字符串作为书签。我用帖子来写数据。这是一个简单的阅读。但根据你的例子,你甚至没有构建一个查询字符串,你希望它如何工作???它确实构建了一个查询字符串,这是错误的。它表示newlist=Server Name&newlist=“我的文本框中有什么”。你能帮忙吗?
/search?ServerName=myservername
public ActionResult Search(string searchType, string searchCriteria)
{
    if(searchType == "PrinterName")
    {
        // search your printers using searchCriteria and return appropriate View
    }
    else if(searchType == "ServerName")
    {
        // search your servers using searchCriteria and return appropriate View
    }
}
@Html.DropDownList("searchType",ViewData["newlist"] as SelectList)
@Html.TextBox("searchCriteria")
<button type="button" onclick="GoToSearch();">Search</button>

function Search() {

    // assumption of jquery (use document.getElementById otherwise...)
    var type = $('#searchType').val();
    var searchCriteria = $('#searchCriteria').val();

    window.location = '@Url.Action("Search", "PrintQueues")' + '?' + type + '=' + searchCriteria;
}