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

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

问题描述

我很确定我在这里做错了什么.在过去的 2 个月中,我们一直在使用 MVC 和 Razor 开发 Web 应用程序,但我们从未想过要使用表单元素.现在母版页和子页已经完成了很多工作,这意味着重构我们的大部分代码以使用表单元素,这将导致页面上有多个表单元素.

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# 代码中的任何控件,我可以给它一个 ID="SomeID" 和一个 RUNAT="SERVER".然后在我后面的代码中我可以设置它的值和属性.

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.

当我在 Razor 中执行此操作时,我们使用以下行:

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.

我真正的问题是,如果我没有表单元素,我该如何在服务器端和客户端做某事".我的印象是,如果我想将值从客户端传递到服务器并返回,最简单的方法是使用隐藏输入.但我只是不知道如何使用 MVC3 和 razor 来实现这一点.

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 和 RUNAT="SERVER",您只是在与网页的构建进行交互.

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 有点相似,因为您在构建模型时拥有服务器端代码(您需要构建用户将看到的数据),但是一旦构建了 HTML,您就需要了解服务器之间的链接并且用户不再存在.他们有一个 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 是只读.您将模型传递到 Razor 页面,该页面将构建适合该模型的 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.

如果你想要一个隐藏元素,它根据是否是第一个视图来设置真或假,你需要在你的模型中使用 bool,如果它是为了响应后续操作,则在 Action 中将其设置为 True.这可以通过根据请求是 [HttpGet] 还是 [HttpPost] 执行不同的操作来完成(如果这适合您设置表单的方式:第一次访问的 GET 请求和提交表单的 POST 请求).

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(因为 bool 默认实例化时为 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.当表单发送回您的服务器时,模型现在将该值设置为 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 迁移到 MVC 的好书可能很重要,例如 Steve Sanderson 的 Pro ASP.NET 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 Razor 隐藏输入和传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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