繁体   English   中英

如何在MVC c#中将HTML选项ID,名称或值从一个视图传递到另一视图的控制器

[英]how to pass html option id or name or value from one view to another view's controller in MVC c#

我正在使用html显示下拉列表。 然后,当用户从列表中选择一个项目时,该项目的ID或名称或其他内容应传递给我的下一个视图的控制器。 这是我的代码...

*****这是我的第一个控制器*****

public ActionResult searchView()
    {
        XElement cruProductRoot = XElement.Load(Server.MapPath("~/XmlFiles/Cruisedata/cruiseprodutstwo.xml"));
        var rootElement = cruProductRoot.Elements("CruiseProduct");//this is the root element
        //for the location field
        var getLocations =  rootElement
                           .Select(l => l.Element("Location").Value)
                           .Distinct();

        var getType = rootElement
                      .Select(t => t.Element("Types").Element("Type").Value)
                      .Distinct();
       // Test productsTestone = new Test();
        List<Test> productsLocation = new List<Test>();
        List<Test> productsType = new List<Test>();
        foreach (var iteml in getLocations)
        {
            productsLocation.Add(new Test
            {
                cruiseLocation = iteml.ToString()

            });
        };

        foreach(var itemt in getType)
        {
            productsType.Add(new Test 
            {
                cruiseType = itemt.ToString(),

            });
        }

        ViewBag.Lc = productsLocation;
        ViewBag.Tp = productsType;
        return View();
    }

***这是控制器的视图****

@using (@Html.BeginForm("Test", "searchView", FormMethod.Post))
{
    <div class="form-group" style="background-color:#808080; padding:30px;">
        <div class="col-md-6" style="margin:10px;">
            <label for="location">Destination </label>
            <select id="location">
                <option>Any</option>
                @foreach (Test item in @ViewBag.Lc)
                {
                    <option value=@item.cruiseLocation>@item.cruiseLocation</option>
                }
                </select>
              </div>
</div>

********这是我的第二个控制器******

public ActionResult resultView(string value)
        {

            XElement sCruise = XElement.Load(Server.MapPath("~/XmlFiles/Cruisedata/cruiseprodutstwo.xml"));
            var rootEle = sCruise.Elements("CruiseProduct")
                          .Where(s => s.Element("Location").Value == value);

            foreach(var it in rootEle)
            {

            }

            return View();
        }

****当用户从列表中选择一个项目,然后单击“提交”时,所选项目应发送到第二个视图的控制器。 我怎样才能做到这一点。 如果我们仅使用这样的链接。

@Html.ActionLink(@itme.cruiseLocation,"resultView", new {name =@item.cruiseLocatin})

我也试过<option>@Html.ActionLink</option> 帮我这个

在第一个控制器集中

TempData["optionid"]=selectedid

在第二个控制器中,您可以从var a=TempData["optionid"]访问该ID

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM