如何将下拉值从主视图传递到控制器并在MVC 4中查看部分视图上的选定值 [英] How to pass dropdown value from master view to controler and view selected value on partial view in MVC 4
问题描述
您好先生,
我想在一个带有下拉列表的搜索页面上,我想在局部视图上显示搜索项目,但是当我选择下拉列表并单击搜索时,我没有找到值我的控制器页面上的下拉列表。
有很多方法可以做到这一点,但要点是你需要从通过搜索的动作渲染局部视图无论如何,他是一个粗略的例子
型号
public class SearchTerm
{
public string SearchID { get < /跨度>; set ; }
public string SearchText { get 跨度>; set ; }
}
public class SearchModel
{
public string SearchValue { get ; set ; }
public SelectList SearchTerms { get ; set ; }
}
index.cshtml
@model Models.SearchModel
@using(Html.BeginForm())
{
< p>
@ Html.DropDownListFor(m => m.SearchValue,Model.SearchTerms)
< / p>
< p>
< input type =submitvalue =搜索/>
< / p>
}
@if(模型!= null)
{
@ Html.Action(搜索,主页,新{searchTerm = Model.SearchValue })
}
_searchResults.cshtml
< pre lang =C#> @ model List< string>
@if(Model == null )
{
return 跨度>;
}
@foreach(字符串结果 模型)
{
< div> @result < / div >
}
控制器
private SelectList GetTerms()3,SearchText = 三个}
{
返回 new SelectList( new 列表< SearchTerm>
{
new SearchTerm {SearchID = 1,SearchText = 一个},
new SearchTerm {SearchID = 2,SearchText = 两个},
new SearchTerm {SearchID = < span class =code-string>
}, SearchID, SearchText);
}
[HttpGet]
public ActionResult Index()
{
SearchModel model = new SearchModel();
model.SearchTerms = GetTerms();
return 查看(模型);
}
[HttpPost]
public ActionResult Index(SearchModel model)
{
model.SearchTerms = GetTerms();
return 查看(模型);
}
public ActionResult搜索( string searchTerm)
{
if ( string .IsNullOrWhiteSpace(searchTerm))
{
return new EmptyResult();
}
// 在此处搜索
List< string> results = new 列表< string> { 结果1代表 + searchTerm, 结果2为 + searchTerm};
return PartialView( 〜/ Views / Home / _searchResults.cshtml,结果);
}
Hello sir,
I want to make a searchin page with dropdown and I want to show search items on a partial view but when I select dropdown and click search then I do not found the value of dropdown on my controller page .
Lots of ways to do this, but the gist is that you need to render your partial view from an action that you pass the search term to.
Anyway, he's a rough example
models
public class SearchTerm { public string SearchID { get; set; } public string SearchText { get; set; } } public class SearchModel { public string SearchValue { get; set; } public SelectList SearchTerms { get; set; } }
index.cshtml
@model Models.SearchModel @using(Html.BeginForm()) { <p> @Html.DropDownListFor(m => m.SearchValue, Model.SearchTerms) </p> <p> <input type="submit" value="Search"/> </p> } @if(Model != null) { @Html.Action("Search", "Home", new { searchTerm = Model.SearchValue }) }
_searchResults.cshtml
@model List<string> @if(Model == null) { return; } @foreach(string result in Model) { <div>@result</div> }
Controller
private SelectList GetTerms() { return new SelectList(new List<SearchTerm> { new SearchTerm{SearchID="1", SearchText="One"}, new SearchTerm{SearchID="2", SearchText="Two"}, new SearchTerm{SearchID="3", SearchText="Three"} }, "SearchID", "SearchText"); } [HttpGet] public ActionResult Index() { SearchModel model = new SearchModel(); model.SearchTerms = GetTerms(); return View(model); } [HttpPost] public ActionResult Index(SearchModel model) { model.SearchTerms = GetTerms(); return View(model); } public ActionResult Search(string searchTerm) { if (string.IsNullOrWhiteSpace(searchTerm)) { return new EmptyResult(); } // do your search here List<string> results = new List<string> { "Result 1 for " + searchTerm, "Result 2 for " + searchTerm }; return PartialView("~/Views/Home/_searchResults.cshtml", results); }
这篇关于如何将下拉值从主视图传递到控制器并在MVC 4中查看部分视图上的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!