Asp.net MVC获得完全限定URL的操作方法 [英] Asp.net MVC get fully qualified URL to an action method

查看:98
本文介绍了Asp.net MVC获得完全限定URL的操作方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个项目中现有的code使用Url.Action获得一个完全合格的URL在对话框中显示。所以它有一个控制器功能,看起来像:

Existing code in a project is using Url.Action to obtain a fully qualified URL to display in a dialog. So it has a controller function that looks like:

public ActionResult CheckItem(bool isCorrect, string id){}

然后Url.Action很简单:

and then Url.Action is simply:

Url.Action("CheckItem", new { isCorrect =  true, id = 2})

现在这一切都工作得很好。但是,我要送一个List<对象>,我有这个全部由提交表单的工作

Now this all works just fine. But I have to send a List<> of objects, I have this all working by submitting a form.

所以我的问题是:有没有使用提交一个Url.Action形式的一种方式?如果没有什么是我的提交表单并获取URL后面的最佳途径。

So my question is: Is there a way of submitting a form using Url.Action? If not what is the best way of submitting my form and getting the URL back.

感谢。

推荐答案

我不明白正是你需要的,但我认为有从用户使用可能复选框或其他一些项目的选择,你。

I didn't understand exactly what you need, but I think that you have some items selection from the user maybe using checkboxes or whatever.

答案可能是此链接:的CheckBoxList在MVC3.0

基本上你有什么做的是:创建一个接收列表或一个IEnumerable的项目的操作,把你的形式张贴到这一行动

Basically what do you have to do is: create an Action that receives a List or a IEnumerable items and put your form to POST to that Action.

我做了一个样本code也可以帮助:

I made a sample code too that could help:

您可以有一个产品型号:

You could have an Item model:

using System;

namespace SandboxMvcApplication.Models
{
    public class Item
    {
        public int Id { get; set; }
        public string Title { get; set; }
    }
}

您控制器可以是:

public class HomeController : Controller
{
    List<Item> itemList = new List<Item>() {
            new Item() { Id = 1, Title = "Item 1" },
            new Item() { Id = 2, Title = "Item 2" },
            new Item() { Id = 3, Title = "Item 3" }
        };

    public ActionResult Index()
    {
        return View(itemList);
    }

    public ActionResult ProcessForm(int[] items)
    {
        var selectedItems = new List<Item>();
        foreach (var item in items)
        {
            selectedItems.AddRange(itemList.Where(i => i.Id == item));
        }

        return View("Success", selectedItems);
    }
}

索引视图(〜/查看/主页/ Index.cshtml):

An Index View (~/Views/Home/Index.cshtml):

@model List<SandboxMvcApplication.Models.Item>

@{
    ViewBag.Title = "Home Page";
}

<form action="@Url.Action("ProcessForm")" method="post">
    <ul>
        @foreach (var item in Model)
        {
            <li><input type="checkbox" name="items" value="@item.Id" />@item.Title</li>
        }
    </ul>

    <input type="submit" value="Send selected items"/>
</form>

最后一个成功的视图来显示用户选择哪些项目:

Finally a success view to show which items the user have selected:

@model List<SandboxMvcApplication.Models.Item>

@{
    ViewBag.Title = "Success";
}

<h2>Success: Selected items were</h2>

<ul>
@foreach (var item in Model)
{
    <li>@item.Id => @item.Title</li>            
}
</ul>

这篇关于Asp.net MVC获得完全限定URL的操作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆