简体   繁体   English

在MVC5中的URL中传递变量

[英]Passing variables in URL in MVC5

I am coding MVC5 and C#. 我正在编码MVC5和C#。

I have an Index View that simply has a dropdownlist that is populated by a controller - with two table fields (File_Desc & Track_Nmbr). 我有一个索引视图,该视图仅具有一个由控制器填充的下拉列表-带有两个表字段(File_Desc和Track_Nmbr)。 When a user selects a File_Desc from the dropdown, it triggers a javascript to redirect and pass the Track_Nmber as 'keyword' to a view called 'List'. 当用户从下拉列表中选择File_Desc时,它会触发JavaScript进行重定向,并将Track_Nmber作为“关键字”传递给名为“列表”的视图。 List then shows all items in the database that have the same Track_Nmbr. 然后,列表显示数据库中具有相同Track_Nmbr的所有项目。 This part all works as designed. 这部分全部按设计工作。

Where I get lost is the next step. 我迷路的地方是下一步。 From the LIST view, after users review current items, they are allowed to enter a new item associated to this Track_Nmbr. 在列表视图中,用户查看当前项目后,可以输入与该Track_Nmbr关联的新项目。 So, I want the Create View to open and pre-populate the Track_Nmbr field. 因此,我希望创建视图打开并预填充Track_Nmbr字段。

I can grab the TRACK_NMBR easy enough from the index view dropdown and pass it to the List View. 我可以从索引视图下拉菜单中轻松捕获TRACK_NMBR并将其传递给列表视图。 But how do I pass it again to the Create View? 但是如何将其再次传递给“创建视图”? Because I can no longer grab it from the dropdown. 因为我无法再从下拉列表中抓住它。

I have been searching and reading, but time is of the essence. 我一直在搜索和阅读,但是时间至关重要。 I will still continue to find information and try my best to resolve it, but I am at a brick wall. 我仍然会继续寻找信息并尽力解决它,但我处在困境中。 I am very new to MVC5. 我对MVC5非常陌生。

I will share all of my code, so that you can see how I have set things up so far. 我将共享我的所有代码,以便您可以了解到目前为止我是如何进行设置的。 I am looking for the easiest solution to open the Create View and prepopulate the TRACK_NMBR Field. 我正在寻找打开“创建视图”并预填充TRACK_NMBR字段的最简单解决方案。

Code: Index Controller 代码:索引控制器

   public ActionResult Index()
    {

        // Create Drop Down List from Data_Catalog

        {
            List<SelectListItem> testList = db.DATA_CATALOG.Select(x => new SelectListItem { Value = x.TRACK_NMBR.ToString(), Text = x.FILE_DESC, Selected = false }).DistinctBy(p => p.Text).ToList();
            ViewBag.testList = new SelectList(testList, "Value", "Text");
            return View();
        }

        // End Drop Down List

    }

Code: Index View 代码:索引视图

@section scripts{

<script type="text/javascript">
    $(document).ready(function () {

        $("#testList").on("change", function () {
            window.location.href = "@Url.Action("Create")?keyword=" + $("#testList").val();



        });
    })

</script>
}

@Html.DropDownList("testList", null, "Select File")

Code: List Controller 代码:列表控制器

 public async Task<ActionResult> List(string searchString, int keyword)


    {

            //Pass TRACK_NMBR to LIST via url

            TRACK_NMBR_ViewModel vm = new TRACK_NMBR_ViewModel();
            vm.TRACK_NMBR = keyword;


        var catalogs = from m in db.FILE_RCPTS_LOG
                       select m;
        {
            catalogs = catalogs.Where(s => s.TRACK_NMBR.Equals(keyword));
        }

        return View(await catalogs.ToListAsync());

        }

 public class TRACK_NMBR_ViewModel
    {
        public int TRACK_NMBR { get; set; }
        public string DISPOSITION { get; set; }
        public DateTime DISPOSITION_DT { get; set; }
        public string PROCESSED_BY { get; set; }
        public DateTime DT_PROCESSED { get; set; }
        public string TIMEFRAME_DESC { get; set; }
        public string DTF_FILE_NAME { get; set; }
    }

Code: List View 代码:列表视图

@section scripts{
<script type="text/javascript">
    function myFunction () {
        window.location.href = "@Url.Action("Create")?keyword=" + $("#testList").val();      
    }

</script>
}

 @Html.ActionLink("Create New", "Create")


@Html.CheckBox("someID", new { @onclick = "myFunction();", htmlAttributes = new { @class = "form-control" } })

As you can see, I placed a checkbox on the view. 如您所见,我在视图上放置了一个复选框。 When user clicks box to add new File_Desc. 当用户单击框以添加新的File_Desc时。 I want to (once again) send the Keyword (track_nmbr) to the Create View and pre-populate the Track_Nmbr Field. 我想(再次)将关键字(track_nmbr)发送到“创建”视图并预先填充Track_Nmbr字段。 But, my keyword is coming up undefined now. 但是,我的关键字现在不确定。 And I can't figure out how to grab it from the url and pass it to the Create View as I did in the Index to List view. 而且我不知道如何像从“索引到列表”视图中那样从网址中获取它并将其传递给“创建”视图。

If I can just send the url with the keyword to the Create View, I can get the rest. 如果我可以将带有关键字的url发送到“创建视图”,则可以获取其余信息。

Again, pretty new with MVC5 and I am getting a major headache. 同样,对于MVC5来说还很新,我感到非常头疼。 C# is also new to me, as I come from a VB.net background. C#对我来说也是新手,因为我来自VB.net。 However, I found it impossible to find good examples in MVC5 using vb.net, and so I am also addressing the learning curve of c#. 但是,我发现无法使用vb.net在MVC5中找到好的示例,因此,我也在解决c#的学习问题。

You need to basically pass the track number from your List action method to the view so that it can be used to create the Create link url where you can pass the track number value as a querystring item 您基本上需要将Track编号从List动作方法传递到视图,以便可以使用它创建Create链接url,在其中您可以将Track编号值作为querystring项进行传递

Since you are not using a view model, you can use ViewBag to pass this data. 由于未使用视图模型,因此可以使用ViewBag传递此数据。

public async Task<ActionResult> List(string searchString, int keyword)
{
  //Your existing code
  ViewBag.Track = keyword;
  return View(await catalogs.ToListAsync());
} 

and now in your view, you can use one of the ActionLink overloads which takes a route value dictionary object. 现在,在您的视图中,您可以使用ActionLink重载之一,该重载采用路由值字典对象。

@Html.ActionLink("Create New", "Create","Home",new { keyword = ViewBag.Track }, null)

This will create an anchor tag with href value which has a querystring item with key keyword and it's value will be whatever value you received in the List action method. 这将创建一个带有href值的锚标记,其中包含一个带有key keyword的querystring项,并且该值将是您在List操作方法中收到的任何值。

Make sure your Create action method has a parameter to accept the querystring value 确保您的Create操作方法具有一个参数以接受querystring值

public ActionResult Create(int keyword)
{
  // to do : use keyword value
  // to do : return something
}

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

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