简体   繁体   English

MVC中带有代码的ASP.Net WebForms用户控件

[英]ASP.Net WebForms User Controls With Code Behind In MVC

I'm trying to move from the Old WebForms .NET approach to the newer MVC version. 我正在尝试从旧的WebForms .NET方法过渡到较新的MVC版本。 I just can't seem to find a solution to this issue. 我似乎找不到解决此问题的方法。

In my current projects I use a lot of custom made User Controls. 在当前的项目中,我使用了许多定制的用户控件。 These controls will in almost all cases have several properties that are populated as parameters in the code behind of the parent 'aspx' page. 在几乎所有情况下,这些控件都将具有几个属性,这些属性将作为参数填充在父“ aspx”页面后面的代码中。

The User Control will have an 'ascx' page where all the html and controls exist. 用户控件将具有一个“ ascx”页面,其中所有的html和控件都存在。 There will also be a 'ascx.cs' file attached to it where all the properties, methods and back-end logic occur. 还将附加一个“ ascx.cs”文件,其中包含所有属性,方法和后端逻辑。

What I can't seem to work out is how this logical process works in MVC? 我似乎无法弄清楚这个逻辑过程在MVC中是如何工作的? The .ascx file is similar to an MVC PartialView... that does make sense. .ascx文件类似于确实有意义的MVC PartialView...。

But where do you store all the backend logic for a PartialView? 但是,您将PartialView的所有后端逻辑存储在哪里? How do I set multiple properties and construct the View based on these values? 如何设置多个属性并基于这些值构造视图?

I've seen some people suggesting you can still use .ascx files in MVC but I'm not sure this is the correct route to go down... certainly not the best practice route anyway? 我见过有人建议您仍然可以在MVC中使用.ascx文件,但我不确定这是否是正确的选择……肯定不是最佳实践吗?

I'll give a small example which may help: 我将举一个可能会有所帮助的小例子:

country.aspx country.aspx

<%@ Register tagprefix="CUSTOM" tagname="Weather" src="~/controls/Weather.ascx" %>

<CUSTOM:Weather ID="Weather" runat="server"></CUSTOM:Weather>

country.aspx.cs country.aspx.cs

Weather.W_CountryCode = CountryCode;
Weather.W_CountryName = CountryName;

weather.ascx.cs weather.ascx.cs

public string W_CountryCode { get; set; }
public string W_CountryName { get; set; }

Ok that is very basic structure of a control. 好的,这是控件的非常基本的结构。

  • The control is embedded into the parent page. 该控件嵌入到父页面中。
  • Parameters are set in the code behind of the parent page. 参数在父页面后面的代码中设置。
  • The properties in the control will be used to collect the selected countries weather data from the database as well as running various other methods. 控件中的属性将用于从数据库中收集所选国家的天气数据,以及运行其他各种方法。

This easily reusable self contained code... I just can't see how you do the same thing in MVC? 这个易于重用的自包含代码...我只是看不到您如何在MVC中做同样的事情? Where do you set the parameters... where is the code behind for the View stored? 您在哪里设置参数...视图的后面代码存储在哪里?

Thanks in advance for any help 预先感谢您的任何帮助

When you create a "page" in MVC, you have a controller action that builds a model and passes it to a view . 在MVC中创建“页面”时,您将具有controller action ,该controller action将构建model并将其传递给view

A "partial" page works exactly the same way - you have a controller action that builds a model and passes it to a view . “部分”页面的工作方式完全相同 -您有一个controller action ,该controller action将构建model并将其传递给view

The only difference is that the action returns View() or PartialView() . 唯一的区别是该操作返回View()PartialView()

When you want to re-use the partial, you can do so in two ways - load via the action or load via the partial. 当您要重用部分时,可以通过两种方式进行操作-通过操作加载或通过部分加载。 When you load via the action @Html.Action , you call the controller-action (perhaps with parameters) and that action builds the model and returns the (partial) view. 当通过@Html.Action操作加载时,调用controller-action(可能带有参数),该操作将构建模型并返回(部分)视图。 When you load via @Html.Partial your view passes the model to the partial directly (ie not via a controller). 通过@Html.Partial加载时,视图将模型直接传递给局部(即,不通过控制器)。 Either is acceptable, it depends on how you are building the partial and whether you've already loaded some data or not etc. 哪种都可以接受,这取决于您如何构建部分数据以及是否已经加载了一些数据等等。

So, for your example: 因此,以您的示例为例:

StaffViewModel.cs (partial) StaffViewModel.cs (部分)

public string W_CountryCode { get; set; }
public string W_CountryName { get; set; }

Staff.cshml (partial) Staff.cshml (部分)

@model StaffViewModel
<div>Country: @Model.W_CountryCode / @Model.W_CountryName</div>

CountryViewModel.cs (view) CountryViewModel.cs (查看)

public IList<StaffViewModel> StaffList { get; set;}

Country.cshtml (view) Country.cshtml (查看)

@model CountryViewModel
@foreach (var staff in Model.StaffList) 
{
    @Html.Partial("Staff", staff)
}

Controller.cs Controller.cs

public ActionResult Country() 
{
    var model = new CountryViewModel();
    model.StaffList = new List<StaffViewModel>();
    // populate staff list from DB etc here

    return View(model);
}

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

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