C# 如何访问和确认我的SelectedListItem文本从我的dropdownlist从一个视图传输到另一个视图?
我通过填写C# 如何访问和确认我的SelectedListItem文本从我的dropdownlist从一个视图传输到另一个视图?,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我通过填写DropDownListFor()的IEnumerable列表来启动索引页。然后我将模型发送到索引视图。 底部的照片显示了两个文本框和下拉列表的索引外观 一旦按下提交按钮,我希望在关于页面中打印信息,以确保信息正在传输。具体地说,我想要从DropDownListFor()中选择的SelectedListItem的文本 我尝试使用input.SelectedText=input.SelectedType.TEXT行将所选值的文本传输到ABOUT视图。这在ABOUT默认操作中,但它从不返回
DropDownListFor()
的IEnumerable
列表来启动索引页。然后我将模型发送到索引视图。
底部的照片显示了两个文本框和下拉列表的索引外观
一旦按下提交按钮,我希望在关于页面中打印信息,以确保信息正在传输。具体地说,我想要从DropDownListFor()
中选择的SelectedListItem
的文本
我尝试使用input.SelectedText=input.SelectedType.TEXT行将所选值的文本传输到ABOUT视图代码>。这在ABOUT默认操作中,但它从不返回我设置的字符串。为什么我的SelectedListItem
没有传输
型号
public class UserInputModel
{
public SelectListItem SelectedType { get; set; }
public IEnumerable<SelectListItem> Type { get; set; }
public string SelectedText { get; set; }
[Required]
public string Path { get; set; }
[Required]
public string Find{ get; set; }
public IEnumerable<SelectListItem> FillType(IEnumerable<SelectListItem> list)
{
list = new[]
{
new SelectListItem { Value = "1", Text = "Xml" },
new SelectListItem { Value = "2", Text = "Html" },
new SelectListItem { Value = "3", Text = "Css" },
new SelectListItem { Value = "4", Text = "C#" }
};
return list;
}
}
查看“索引”
@使用(Html.BeginForm(“Index”,“Find”,FormMethod.Post))
{
@Html.AntiForgeryToken()
@ValidationSummary(true,“你好”,new{@class=“text danger”})
@LabelFor(model=>model.SelectedType,htmlAttributes:new{@class=“controllabel col-md-2”})
@Html.DropDownListFor(model=>model.SelectedType,model.Type,“选择文件类型”,新建{@class=“form control”,name=“Type”})
@LabelFor(model=>model.Path,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Path,new{htmlAttributes=new{@class=“form control”,name=“Path”})
@Html.ValidationMessageFor(model=>model.Path,“,new{@class=“text danger”})
@*
@LabelFor(model=>model.SelectedFolder,htmlAttributes:new{@class=“controllabel col-md-2”})
@DropDownListFor(model=>model.SelectedFolder,newselectlist(model.DirectoryFolders),“选择文件夹:”,new{@class=“form control”})
*@
@LabelFor(model=>model.Find,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Find,new{htmlAttributes=new{@class=“form control”,name=“Find”})
@Html.ValidationMessageFor(model=>model.Find,“,new{@class=“text danger”})
}
查看“关于”
选定:@Model.SelectedText
路径:@Model.Path
查找:@Model.Find
关于好的和坏的实践的任何其他反馈都会很有帮助,因为这是我第一次使用MVC。A
会发回所选选项的值,而不是它的显示文本。您不能将
绑定到复杂对象(这就是SelectedType
的含义)-您需要将属性更改为public int SelectedType{get;set;}
-如果您希望在post方法中显示文本,请再次查找。这非常有用。再次感谢@StephenMuecke。
public class FindController : Controller
{
public ActionResult Index()
{
var input = new UserInputModel();
input.Type=input.FillType(input.Type);
return View(input);
}
[HttpPost]
public ActionResult Index(UserInputModel input)
{
return View("About", input);
}
public ActionResult About(UserInputModel input)
{
input.SelectedText = input.SelectedType.Text;
return View(input);
}
}
@using (Html.BeginForm("Index", "Find", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "Hello There", new { @class = "text-danger" })
<input type="hidden" id="hidText" name="hidText" />
<div class="form-group">
@Html.LabelFor(model => model.SelectedType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.SelectedType, Model.Type, "Select a File Type", new { @class = "form-control", name = "type" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Path, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Path, new { htmlAttributes = new { @class = "form-control", name = "path" } })
@Html.ValidationMessageFor(model => model.Path, "", new { @class = "text-danger" })
</div>
</div>
@* <div class="form-group">
@Html.LabelFor(model => model.SelectedFolder, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.SelectedFolder, new SelectList(Model.DirectoryFolders), "Select a Folder:", new { @class = "form-control" })
</div>
</div>
*@
<div class="form-group">
@Html.LabelFor(model => model.Find, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Find, new { htmlAttributes = new { @class = "form-control", name = "find" } })
@Html.ValidationMessageFor(model => model.Find, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Submit" class="btn btn-default" />
</div>
</div>
</div>
}
<body>
<div>
<h3>Selected:@Model.SelectedText</h3>
<h3>Path:@Model.Path</h3>
<h3>Find:@Model.Find</h3>
</div>
</body>