English 中文(简体)
ASP .NET MVC 3 - 通过从观点到主计长的参数
原标题:ASP .NET MVC 3 - Passing Parameters from View to Controller

如果你将匿名物体退回控制员,则通过查阅匿名物体财产名称来掩饰控制者的论点,那么,我如何将相关物体的特性退回到我的模型中,以便违约价值对控制者具有约束力?

For example:

Model:

public class MyViewModel
{
    public MyItem MyItem { get; set; }
}

public class MyItem
{
    public string Name { get; set; }
}

Controller:

public ActionResult Index(MyViewModel model)
{
    return View(model);
}

In the view, I want to pass the MyItem.Name back, how is this possible? I have tried:

@Html.ActionLinke("Index", "MyController", new { name = "example" })

and

@Html.ActionLinke("Index", "MyController", new { myItem_Name = "example" })

Help please!

最佳回答

答案是创建一种惯例模型Binder和模型BinderProvider。

问题回答

您:

@Html.ActionLink("Index", "MyController", new { name = @Model.MyItem.Name })

在行动方法中:

public ActionResult Index(string name)
{
    return View();
}

您可使用以下超载:

@Html.ActionLink( 
    "link text",                             // linkText
    "Index",                                 // actionName
    "MyController",                          // controllerName
    new RouteValueDictionary {               // routeData
        { "MyItem.Name", "example" } 
    }, 
    null                                     // htmlAttributes
)

I think you may be misunderstanding the passing of object from view to controller . It s not that you re passing object - you re just rendering a link (anchor tag) in the page. The request will actually come from the browser when the user clicks the link. There is no server-side object passing going on.

链接必须包含服务器(控制器)作为查询参数所需要的所有参数。

我认为,你真的想要做的是:

  1. render a form and submit it, or

  2. 仅通过你所需要的数据,在控制器检索数据(例如从非行取)





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签