简体   繁体   English

将CSS类从控制器传递到ASP.NET MVC3中的局部视图

[英]Pass a css class from controller to a partial view in asp.net mvc3

I have a simple code from controller 我有一个来自控制器的简单代码

public ActionResult CreatePage() {

   return PartialView( "APage" );
}

and the part of that page APage is: 该页面APage的一部分是:

<table class="@className">
  <tr>
  ...
  </tr>
</table>

In javascript, I want to generate APage with different class name (css class name) 在javascript中,我要生成具有不同类名(css类名)的APage

$.post('CreatePage', function(data) {
  $('.result').html(data);
});

How to pass in controller function (if I would declare : public ActionResult CreatePage(string cssClass) { ... } ) the parameter to PartialView function ? 如何将控制器函数(如果我要声明: public ActionResult CreatePage(string cssClass) { ... } )传递给PartialView函数?

Means 手段

I want like: 我想要:

public ActionResult CreatePage( string cssClass ) {

       return PartialView( "APage", cssClass );
    }

And I want to use that css class into APage view. 而且我想在APage视图中使用该CSS类。

For example: 例如:

  1. If I call 如果我打电话

    $.post('CreatePage', {cssClass: 'aClass' ,function(data) { $('.result').html(data); });

  2. Then it will call 然后它将调用

     public ActionResult CreatePage( string cssClass ) { return PartialView( "APage", cssClass ); //cssClass = 'aClass' } 
  3. And return the view like 并像返回视图

    <table class="aClass"> <tr> ... </tr> </table>

Thank you 谢谢

I'm not sure if I understood you correctly, but your example, I think, is already on the right track. 我不确定我是否正确理解了您,但是我认为您的榜样已经走上正确的道路。

In your partial view, add this at the very top: 在您的局部视图中,将此添加到最顶部:

@model string

And then in your partial view, change the table tag definition to 然后在局部视图中,将表标记定义更改为

<table class="@Model"> <tr> ... </tr> </table>

To extend upon what @rikitikitik said. 继续@rikitikitik所说的。

You already discovered the PartialView(viewName, model) method overload, now you just have to extend your current model to include the CSS class string. 您已经发现PartialView(viewName, model)方法重载,现在只需要扩展当前model以包含CSS类字符串即可。 Just add a property called CssClass and you will be able to use it in your partial view. 只需添加一个名为CssClass的属性,您就可以在部分视图中使用它。

This of course assumes you are using view models (and thus the MVVM pattern ), not "just" models or even database models (handled by for example Entity Framework). 当然,这假设您使用的是视图模型 (因此使用MVVM模式 ),而不是“仅”模型甚至数据库模型(例如由Entity Framework处理)。

public class APartialModel
{
    public string Name { get; set; }
    // ... other properties
    public string CssClass { get; set; }
}
public ActionResult CreatePage( string cssClass ) {

   // Initialize the entire view model for the partial view here
   // This usually means you need to pass in an id and use it to
   // make a database lookup.
   // If it's "too much work", it probably means you
   // need to fix a structural problem.
   APartialModel model = new APartialModel
       {
           Name = "Somehow I already know this value",
           CssClass = cssClass
       };

   return PartialView( "APage", model );
}
@model APartialModel

<table class="@Model.CssClass">
  <tr>
  ... for example @Model.Name
  </tr>
</table>

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

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