如何访问并确认我的SelectedListItem TEXT从我的下拉列表从一个视图转移到另一个视图?

时间:2018-06-21 16:38:24

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我通过填写IEnumerable的{​​{1}}列表来启动INDEX页面。然后,将模型发送到INDEX视图。 底部的照片通过两个文本框和下拉列表显示了INDEX的外观。

一旦按下提交按钮,我希望将信息打印在ABOUT页面中,以确保信息正在传输。具体来说,我希望从DropDownListFor()中选择SelectedListItem的TEXT。

我尝试使用行DropDownListFor()将所选值的TEXT传输到ABOUT View。这在ABOUT默认操作中,但是它永远不会返回我设置的字符串。为什么我的input.SelectedText = input.SelectedType.Text;不转账?

模型

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

查看“索引”

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

INDEX View

ABOUT View

由于这是我第一次与MVC合作,因此有关良好和不良做法的任何其他反馈都将有所帮助。

0 个答案:

没有答案