简体   繁体   English

在MVC 2中的View和Controller之间传递值

[英]Passing values between View and Controller in MVC 2

I'm constantly confused about how to pass values between Views and Controllers in MVC. 我一直对如何在MVC中的视图和控制器之间传递值感到困惑。 I know I can set ViewData in the Controller and use that in the View, but what about the other way around? 我知道我可以在Controller中设置ViewData并在View中使用它,但是相反呢?

What I have found is I can use a hidden field and then access it through Request.Form["name"] like this: 我发现我可以使用一个隐藏字段,然后通过Request.Form [“ name”]来访问它,如下所示:

<% using (Html.BeginForm("Upload", "Customers", FormMethod.Post, new { enctype = "multipart/form-data" }))
   {%>
<br />
<input id="currentDir" type="hidden" name="currentDir" value="" />
<input type="file" name="fileTextbox" id="fileTextbox" />
<br />
<br />
<input type="submit" value="Send" />
<% } %>

What complicates it even more is that the value originally comes from a jquery script, so that's why the input field was the only way I could think of. 更为复杂的是,该值最初来自于jquery脚本,因此这就是为什么输入字段是我能想到的唯一方法的原因。 But it still feels wrong... Maybe it isn't but I'd basically like to know if there are other more "proper" established ways to pass values between the View and Controller (both ways). 但这仍然感觉不对...也许不是,但是我基本上想知道是否还有其他“适当的”已建立方法在视图和控制器之间传递值(两种方法)。 Should one use querystrings instead? 应该使用查询字符串代替吗? If so, how would they look in the html.beginform htmlhelper? 如果是这样,它们在html.beginform htmlhelper中的外观如何?

Also, what I'm trying to do here is enable upload possibilities for my application. 另外,我在这里尝试做的是为我的应用程序启用上载可能性。 And I'm trying to make the whole application as "Ajaxy" as possible. 而且我正在尝试使整个应用程序尽可能地“ Ajaxy”。 But this form will make a complete post. 但是此表格将提供完整的帖子。 Is there another way to do this and not have to reload the entire page for this upload? 还有另一种方法可以执行此操作,而不必为此上传重新加载整个页面吗?

Let's ignore the "AJAX-y" aspects for a moment (because that's a different issue) and just look at passing data between views and controllers. 让我们暂时忽略“ AJAX-y”方面(因为这是一个不同的问题),只看一下视图和控制器之间的数据传递。 I would first recommend that you check out the NerdDinner Tutorial which provides some good insights into how MVC works and how you use some of the features of MVC. 我首先建议您查看NerdDinner教程 ,该教程对MVC的工作原理以及如何使用MVC的某些功能提供了一些很好的见解。

To address your specific question of how data is passed from View to Controller and back, there are a few ways to do this. 为了解决有关如何将数据从View传递到Controller以及传递回Controller的特定问题,有几种方法可以做到这一点。 However, the one that tends to make sense to most people is the idea of using strongly-typed views. 但是,对大多数人来说更有意义的是使用强类型视图的想法。

Let's say you have a model called Person. 假设您有一个名为Person的模型。 For now, don't worry about how we store Person data - we just have a Person class in the Models folder inside your MVC project. 现在,不必担心我们如何存储Person数据-我们在MVC项目内的Models文件夹中只有一个Person类。

public class Person {

  public string FirstName;
  public string LastName;

  public Person() {
    FirstName = "John";
    LastName = "Doe";
  }
}

When we want to display data about Person in a View, we make a request to a specific controller. 当我们想在视图中显示有关Person的数据时,我们向特定的控制器发出请求。 In this case (and for clarity) we'll call this controller the MainController. 在这种情况下(为了清楚起见),我们将此控制器称为MainController。 This will go in the Controllers folder and will be called MainController. 这将进入Controllers文件夹,并将其称为MainController。 Let's call the Action (an action is really just a specialized method) we want to get data from Index. 我们称要从索引中获取数据的动作(动作实际上只是一种专门的方法)。 Due to how ASP.NET MVC routing works, the path to our server will be: http://localhost/Main/Index . 由于ASP.NET MVC路由的工作方式,我们服务器的路径将为: http:// localhost / Main / Index Notice the Controller (minus the "Controller" name), and the Action make up the path. 注意Controller(减去“ Controller”名称),然后Action组成路径。 (The first part is your server name, of course.) (当然,第一部分是您的服务器名称。)

Let's look at your controller - I'm going to keep it very simple for now: 让我们看一下您的控制器-现在,我将使其变得非常简单:

public class MainController : Controller {

  public ActionResult Index() {
    Person person = new Person();
    return View(person);
  }
}

What we have going on inside the Index Action is that it is returning a View (which, by default, has the same name as the Action) and a model to correspond with that view. 我们在Index Action中进行的操作是返回一个View(默认情况下,该名称与Action同名)和一个与该视图相对应的模型。 Now, we have to create our view. 现在,我们必须创建我们的视图。

The important part here is that you want to strongly-type the model that is being returned in the controller to your view. 这里的重要部分是您要对在控制器中返回到视图的模型进行强类型输入。 You do that with this line (which is first in your aspx file). 您可以使用此行(在aspx文件中首先显示)执行此操作。

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewUserControl<Project.Namespace.Person>" %>

Notice the "Inherits" attribute and notice that your Person model makes up that attribute. 注意“ Inherits”属性,并注意您的Person模型构成了该属性。

Now, just code the rest of your view as normal. 现在,只需正常编写其余视图即可。 Let's say we want to display the current Person name, and allow someone to change the name. 假设我们要显示当前的“个人”名称,并允许某人更改名称。 The page would look like this (I'm not making this pretty): 该页面看起来像这样(我不是很漂亮):

<% using (Html.BeginForm()) { %>
  <%: Html.LabelFor(model => model.FirstName) %>
  <%: Html.TextBoxFor(model => model.FirstName) %>
  <%: Html.LabelFor(model => model.LastName) %>
  <%: Html.TextBoxFor(model => model.LastName) %>

  <input type="submit" value="Submit" name="submitButton" />
<% } %>

This is the important part about getting data back and forth between Controllers and Views. 这是关于在Controller和View之间来回传输数据的重要部分。 What we are doing here is that we are taking your strongly-typed view (which is typed with the Person class) and using helper methods (like LabelFor and TextBoxFor) to tie the model together with its data and, ultimately, together with the actions contained in the controller (which we have to finish developing here in one moment). 我们在这里所做的是,我们采用您的强类型视图(使用Person类输入),并使用辅助方法(如LabelFor和TextBoxFor)将模型及其数据以及最终与操作绑定在一起包含在控制器中(我们必须马上完成这里的开发)。

So, you can now see the data. 这样,您现在可以看到数据。 But, if a user changes the name and clicks submit - we want the page to display the new name. 但是,如果用户更改名称并单击提交-我们希望页面显示新名称。 This means we need to add one more action to MainController - the one that receives the data. 这意味着我们需要再向MainController添加一个动作-接收数据的动作。

[HttpPost]
public ActionResult Index(Person person) {
  // Do whatever you want with the Person model. Update a database, or whatever.
  return View(person);
}

This action looks very similar to the other action we just developed. 该动作看起来与我们刚刚开发的其他动作非常相似。 However, this one takes a person object (from the form that is being submitted) and it gives the controller an opportunity to do whatever needs to be done with that object. 但是,该对象(从提交的表单中)获取一个人对象,并为控制器提供了机会来处理该对象需要做的任何事情。 Once this is done, you can choose to redirect to a different page, redisplay the page (useful if there are errors), or do any number of other things. 完成此操作后,您可以选择重定向到其他页面,重新显示该页面(如果有错误,则很有用),或执行许多其他操作。

Again, this is ALL covered (and much more) in the NerdDinner Tutorial . 同样,这在NerdDinner教程中进行了介绍(以及更多)。 I highly recommend you read and follow through that. 我强烈建议您阅读并遵循。

As for the AJAX-y aspects you discussed, the premise is still the same (although there is a little bit of JavaScript/jQuery work that goes on in there). 至于您所讨论的AJAX-y方面,前提仍然是相同的(尽管其中进行了一些JavaScript / jQuery工作)。 I won't go into it now, but the basics are also covered in the NerdDinner tutorial . 我现在不再赘述,但NerdDinner教程中也介绍了基础知识。

I hope this gets you started. 我希望这可以帮助您入门。 I remember being a bit confused too when I first started working with web technologies, so I hope this helps you out! 我记得刚开始使用Web技术时也会有些困惑,所以希望这对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM