简体   繁体   中英

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. 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.

The User Control will have an 'ascx' page where all the html and controls exist. There will also be a 'ascx.cs' file attached to it where all the properties, methods and back-end logic occur.

What I can't seem to work out is how this logical process works in MVC? The .ascx file is similar to an MVC PartialView... that does make sense.

But where do you store all the backend logic for a 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?

I'll give a small example which may help:

country.aspx

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

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

country.aspx.cs

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

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? 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 .

A "partial" page works exactly the same way - you have a controller action that builds a model and passes it to a view .

The only difference is that the action returns View() or 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. When you load via @Html.Partial your view passes the model to the partial directly (ie not via a controller). 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)

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

Staff.cshml (partial)

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

CountryViewModel.cs (view)

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

Country.cshtml (view)

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

Controller.cs

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

    return View(model);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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