MVC剃刀隐藏的输入和传递价值 [英] MVC Razor Hidden input and passing values

查看:117
本文介绍了MVC剃刀隐藏的输入和传递价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty确保我错在这里做一些事情。在过去的2个月中,我们一直在开发使用MVC和剃须刀的web应用程序,我们从来没有想过用表单元素。现在这么多已经与母版页和子页面,这意味着重组我们的大多数code,以使用表单元素和将导致页面上的多个表单元素进行。

I am pretty sure I am doing something wrong here. Over the last 2 months we have been developing a web app using MVC and Razor and we never thought of using the form element. Now so much has already been done with master pages and sub pages that it means restructuring most of our code in order to use form element and the would result in multiple form elements on a page.

这一边,在Asp.Net如果我想获得在C#code任何控制后面我可以给它一个ID =SomeID和=服务器。然后在我的code的背后,我可以将其值设置和属性。

That aside, in Asp.Net if I wanted to access any control in the C# code behind I could just give it an ID="SomeID" and a RUNAT="SERVER". Then in my code behind I could set its value and properties.

当我这样做是剃刀,我们使用像行:

When I do this in Razor, we use lines like:

 <input id="hiddenPostBack" runat="server" type="hidden" />

为什么我不能访问该控制器?我想检测回发并将其值设置为false,如果它是第一次加载页面,如果没有,则该值设置为true。然后在此基础上,我会读它无论是服务器端还是客户端,做一些事情。

Why can't I access this in the controller? I want to detect a postback and set the value to false if it is the first time the page loads, and if not, then set the value to true. Then based on this, I will read it either server side or client side and do something.

我的真正的问题是,我怎么做什么因为我没有一个表单元素包括服务器端和客户端。我是IM pression,如果我想从客户端的值传递到服务器和背部,要做到这一点最简单的方法是用一个隐藏的输入下。但我只是没有得到如何与MVC3和剃刀做到这一点。

My real question is, how do I "do something" both server side and client side given that I don't have a form element. I was under the impression that if I wanted to pass values from client to server and back, the easiest way to do this is with a hidden input. But I am just not getting how to accomplish this with MVC3 and razor.

在此先感谢您的帮助。

推荐答案

从WebForms的MVC的一招,需要在逻辑和大脑处理一个完整的巨变。你不再与形式了服务器端和客户端进行交互(事实上,即使你的WebForms没有交互客户端)。你可能只是混了个有点用的WebForms想在那里,在和 =服务器你只是交互的的建筑的的网页。

A move from WebForms to MVC requires a complete sea-change in logic and brain processes. You're no longer interacting with the 'form' both server-side and client-side (and in fact even with WebForms you weren't interacting client-side). You've probably just mixed up a bit of thinking there, in that with WebForms and RUNAT="SERVER" you were merely interacting with the building of the Web page.

MVC是在有点类似你有服务器端code在构建模型(你需要建立自己的用户将看到什么数据),但一旦你已经建立了HTML,你需要AP preciate该服务器和该用户之间的链路不再存在。他们的HTML页面,仅此而已。

MVC is somewhat similar in that you have server-side code in constructing the model (the data you need to build what your user will see), but once you have built the HTML you need to appreciate that the link between the server and the user no longer exists. They have a page of HTML, that's it.

所以,你正在构建的是HTML的只读的。您可以通过给剃刀页面,这将建立适合该模型通过HTML模式。

So the HTML you are building is read-only. You pass the model through to the Razor page, which will build HTML appropriate to that model.

如果你想拥有这台true或false取决于这是否是第一个视图或者不是你需要在你的模型布尔,并将其设置为True行动,如果它在响应跟进一个隐藏的元素。这可以通过根据请求是否是不同的动作有做[HTTPGET]或[HttpPost](如果这是适合您如何设置您的形式:第一个访问,如果提交表单POST请求GET请求)。

If you want to have a hidden element which sets true or false depending on whether this is the first view or not you need a bool in your model, and set it to True in the Action if it's in response to a follow up. This could be done by having different actions depending on whether the request is [HttpGet] or [HttpPost] (if that's appropriate for how you set up your form: a GET request for the first visit and a POST request if submitting a form).

另外,它在创建时的模型可以设置为True(这将是你第一次访问该页面),但是的之后的您检查值是true或false(因为布尔默认为False时,它的实例化)。然后用:

Alternatively the model could be set to True when it's created (which will be the first time you visit the page), but after you check the value as being True or False (since a bool defaults to False when it's instantiated). Then using:

@Html.HiddenFor(x => x.HiddenPostBack)

在您的形式,这将会把一个隐藏的真实。当窗体回发到服务器的模式,现在已经该值设置为True。

in your form, which will put a hidden True. When the form is posted back to your server the model will now have that value set to True.

这是很难给予更多的建议比你的问题不是特定为的为什么的你想这样做。你从的WebForms,如史蒂夫·桑德森公司的Pro ASP.NET MVC移动到MVC读一本好书这也许是至关重要的。

It's hard to give much more advice than that as your question isn't specific as to why you want to do this. It's perhaps vital that you read a good book on moving to MVC from WebForms, such as Steve Sanderson's Pro ASP.NET MVC.

这篇关于MVC剃刀隐藏的输入和传递价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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