如何从视图中值传递给控制器​​在ASP.NET MVC 3(剃刀)? [英] How to pass values from view to controller in ASP.NET MVC 3 (Razor)?

查看:124
本文介绍了如何从视图中值传递给控制器​​在ASP.NET MVC 3(剃刀)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中的表:

http://i.stack.imgur.com/C7CWX.png

和我这个code(查看)显示不repitions在音乐的所有值:

  @foreach(在Model.Select VAR项目(M = GT; M.MUSIC).Distinct())
{
    &所述; TR>
        &所述; TD>
            @ Html.ActionLink(项目,深,计算机[项目])
        < / TD>
    < / TR>
}

结果:

 爵士
迪斯科

金属

和我想,当我pressed爵士的结果是万里,柯克等。
怎么办呢?
我想我可能从视图控制器传递价值,但我不知道如何在我的情况下做到这一点。
请帮我

在控制器动作(例如):

 公众的ActionResult深(弦乐)
    {        VAR解析度=(从d.table m其中M.MUSIC ==选择音乐米);
        返回查看(RES);
    }


解决方案

这是不可能从视图传递值直接控制器。当用户请求Web服务器为你初始页面的Web服务器(ASP.NET)计算出要调用的动作。这一行动收集数据和发送数据(使用视图模型,ViewBag,ViewData的或TempData的),以用于渲染视图。在这种情况下呈现是方法,其中的数据被变换为HTML(可以是任何真CSS,JavaScript中,...在此看作为字节阵列),其是发送到客户端(浏览器),其中除$ P $角它作为网页。用户看到你的链接,当他点击它,他将到Web服务器的新的请求,这个过程重复。

您所创建的code与链接:
@ Html.ActionLink(项目,深,计算机[项目])

第一个参数产品链接文本。 深是行动调用当前控制器上的名称。第三个参数是类型的对象。该第三参数应该是相同类型的深动作的参数和应该使用下一个符号传递数据

@ Html.ActionLink(项目,深,新的{idItem = item.IdMusic,富=酒吧})

在previous例如ASP MVC期待你的深的行动有参数idItem和富。

有一点要注意的是,客户/用户正在对Web服务器的请求,他不能像你试图发送一个对象。相反,你的对象发送唯一标识符(idMusic)并检索它在从数据库/缓存/会话请求。

希望这有助于
兹拉蒂博尔

I have this table in database:

http://i.stack.imgur.com/C7CWX.png

And I display all value in 'music' without repitions by this code(View):

@foreach (var item in Model.Select(m => m.music).Distinct())
{
    <tr>
        <td>
            @Html.ActionLink(item, "Deep", ViewData["item"])
        </td>
    </tr>
}

Result:

Jazz
Disko
Rock
Metal

And I want when I pressed 'Jazz' result was 'Miles','Kirk' etc. How do it? I think I may pass value from View to Controller, but i dont know how do it in my situation. Please help me

Action in controller(for example):

    public ActionResult Deep(string music)
    {

        var res = (from m in d.table where m.music == music select m);
        return View(res);
    }

解决方案

It isn't possible to pass values from view to controller directly. When user asks web server for you initial page web server (ASP.NET) figures out the action to invoke. That action gathers data and sends that data (using view models, ViewBag, ViewData or TempData) to the view for rendering. Rendering in this case is process where your data is transformed to HTML (can be anything really CSS, JavaScript,... look at this as array of bytes) which is send to the client (browser) which interpret it as web page. User sees your link and when he clicks on it he will make the new request to the web server and this process repeats.

You are creating the code for links with: @Html.ActionLink(item, "Deep", ViewData["item"])

First parameter item is link text. "Deep" is name of action to invoke on current controller. Third parameter is of type object. This third parameter should be the same type as the parameters in the Deep action and you should use next notation for passing data

@Html.ActionLink(item, "Deep", new { idItem = item.IdMusic, foo = "bar" })

In previous example ASP MVC expects that your "Deep" action has parameters idItem and foo.

One thing to note is that client/user is making a request to the web server, he cant send an object like you tried. Instead you send unique identifier (idMusic) of object and retrieve it upon request from database/cache/session.

Hope this helps, Zlatibor

这篇关于如何从视图中值传递给控制器​​在ASP.NET MVC 3(剃刀)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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