Как создать основную страницу со строгим контролем типов с помощью основного контроллера в ASP.NET MVC

Я остановился на небольшом изменении кода Джезраэля, который автоматически заботится о количестве индексов. Обратите внимание, что df.columns изначально имеет вид [(x1,y1), (x2,y2), ..., (xn, yn)], где n - количество столбцов, xi - метка столбца i в первой строке заголовка, а yi - одна из вторая строка заголовка.

df = pandas.read_fwf(f, header=[0,1])
cols = [x for x,_ in df.columns if 'Unnamed' not in x]
idxs = [y for _,y in df.columns if 'Unnamed' not in y]
df.columns = idxs + cols
df[idxs] = df[idxs].ffill()
df.set_index(idxs, inplace=True)
20
задан George Stocker 3 January 2010 в 22:12
поделиться

2 ответа

You could create an after action executed filter which looks for a model of that type and sets the properties accordingly, perhaps by calling a base controller function. You would then put the filter on the base class, and all actions would see it automatically.

The action filter attribute gets the controller's ViewModel, and passes it to the controller's SetModel function:

using System.Web.Mvc;
using Site1.Controllers;

namespace Site1.Models
{
    public class MasterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);

            MasterViewModel viewModel = (MasterViewModel)((ViewResultBase)filterContext.Result).ViewData.Model;

            BaseController controller = (BaseController)filterContext.Controller;
            controller.SetModel(viewModel);
        }
    }
}

This function is added to the BaseController:

public void SetModel(MasterViewModel childViewModel)
{
    childViewModel.Buttons = model.Buttons;
}
17
ответ дан 30 November 2019 в 01:02
поделиться

Rather than creating an attribute, why not just override Controller.OnActionExecuted and put the initialization code there? Seems a bit simpler.

6
ответ дан 30 November 2019 в 01:02
поделиться
Другие вопросы по тегам:

Похожие вопросы: