小编Bre*_*ogt的帖子

ASP.NET MVC 3如何将Javascript注入主布局

我正在尝试从MVC 3新的剃刀视图引擎.我遇到的问题是我有特定于页面的Javascript.我通常在标签关闭之前拥有所有的Javascript代码.我想在我关闭主布局上的body标签之前放置一个部分.有些事情的影响:

<script type="text/javascript">
   @RenderSection("JavaScript")
</script>
Run Code Online (Sandbox Code Playgroud)

但VS2010以绿色突出显示它.所以哪个页面使用这个主布局都可以在这里注入Javascript.你们会怎么做?我想这样做的原因是因为我可以在这里添加来自主布局的JavaScript,其他我将不得不在@RenderSection下面定义一个单独的脚本标记.

当我执行以下操作时,VS会给我一个警告(我不喜欢警告):

Validation (HTML 4.01): Element 'link' cannot be nested within element 'link'.
Run Code Online (Sandbox Code Playgroud)

这是我上面警告的代码:

@section HeadSection
{
    <link href="http://yui.yahooapis.com/2.8.2r1/build/button/assets/skins/sam/button.css" rel="stylesheet" type="text/css">
    <link href="http://yui.yahooapis.com/2.8.2r1/build/datatable/assets/skins/sam/datatable.css" rel="stylesheet" type="text/css">
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能得到这些警告?

javascript razor asp.net-mvc-3

4
推荐指数
1
解决办法
7704
查看次数

RedirectToAction替代方案

我正在使用ASP.NET MVC 3.

我按照以下方式编写了一个帮助类:

public static string NewsList(this UrlHelper helper)
{
     return helper.Action("List", "News");
}
Run Code Online (Sandbox Code Playgroud)

在我的控制器代码中我使用它像这样:

return RedirectToAction(Url.NewsList());
Run Code Online (Sandbox Code Playgroud)

所以在重定向之后,链接看起来像这样:

../News/News/List
Run Code Online (Sandbox Code Playgroud)

RedirectToAction有替代品吗?有没有更好的方法来实现我的帮助方法NewsList?

asp.net asp.net-mvc asp.net-mvc-3

4
推荐指数
1
解决办法
4861
查看次数

在整数属性上使用NotEmpty进行流畅验证

我有以下代码:

public class NewsEditViewDataValidator : AbstractValidator<NewsEditViewData>
{
     public NewsEditViewDataValidator()
     {
          // Status unique identifier cannot be empty
          // Status unique identifier must be greater or equal to 1
          RuleFor(x => x.StatusId)
               .NotEmpty()
               .WithMessage("Status is required")
               .GreaterThanOrEqualTo(1)
               .WithMessage("Status unique identifier must be greater or equal to 1");

          // Other rule sets
     }
}
Run Code Online (Sandbox Code Playgroud)

StatusId是一个整数.NotEmpty在这种情况下如何工作?它验证了什么?整数还是字符串?对于此部分检查整数是否为空,单元测试会是什么样的?

这用于验证我的MVC 3应用程序中的下拉列表.验证在视图上​​运行良好.GreaterThanOrEqualTo部分是状态唯一标识符永远不会小于1.这我想触发验证我的对象.什么时候这样做NotEmpty也不会开火?是否首先要解雇哪一个?如果StatusId为0,哪个规则集将触发?如果是-1?我希望NotEmpty在检查业务对象的id时使用视图和GreaterThanOrEqualTo.有什么建议?

asp.net-mvc fluentvalidation

4
推荐指数
1
解决办法
1万
查看次数

数字问题的FluentValidation

我在ASP.NET MVC 3应用程序中使用FluentValidation.

我的视图模型中有一个MaxNumberTeamMembers属性:

/// <summary>
/// Gets or sets the maximum number of team members.
/// </summary>
public int MaxNumberTeamMembers { get; set; }
Run Code Online (Sandbox Code Playgroud)

我想知道以下规则集是否可行:

  • 在前端视图中,如果文本框为空,那么我想要显示"需要MaxNumberTeamMembers"消息
  • 如果输入的数字小于1,那么我想要一条消息显示"MaxNumberTeamMembers应该大于或等于1".

上面的规则集会是什么样的?

我有以下内容,但如果我输入0,它在GreaterThan部分不起作用:

RuleFor(x => x.MaxNumberTeamMembers)
     .NotEmpty()
     .WithMessage("Max. number of team members is required")
     .GreaterThan(0)
     .WithMessage("Max. number of team members must be greater than 0");
Run Code Online (Sandbox Code Playgroud)

更新2011-02-14:

RuleFor(x => x.MinNumberCharactersCitation)
   .NotNull()
   .WithMessage("Min. number of characters for citation is required")
   .GreaterThanOrEqualTo(1)
   .WithMessage("Min. number of characters for citation must be greater than or equal to 1") …
Run Code Online (Sandbox Code Playgroud)

validation asp.net-mvc fluentvalidation asp.net-mvc-3

4
推荐指数
1
解决办法
1万
查看次数

如何首先获取实体框架代码中的自动递增值

我在用Entity Framework 4.1 code first.我有一个产品表,其Id列是一个自动增量列.当我添加Product实例时,如何获取此新ID或更新的Product(返回属性)?

我的存储库代码:

MyContext db = new MyContext();

public void Insert(Product product)
{
     db.Products.Add(product);
     db.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)

c# sql-server ado.net entity-framework entity-framework-4.1

4
推荐指数
1
解决办法
4337
查看次数

在Telerik MVC AJAX绑定属性列上使用自定义日期格式化程序

Telerik MVC在我的ASP.NET MVC3应用程序上使用最新版本的控件razor.

我已经定义了我的网格结构如下:

@(Html.Telerik()
     .Grid<GrantApplicationListViewModel>()
     .Name("grdGrantApplications")
     .Columns(column =>
     {
          column.Bound(x => x.FullName)
               .Title("Owner")
               .Width(200);

          column.Bound(x => x.CreatedDate)
               .Title("Created")
               .Width(90);
     })
     .DataBinding(dataBinding => dataBinding.Ajax().Select("AjaxGrantApplicationsBinding", "Home"))
     .Pageable(paging => paging.PageSize(30))
     .TableHtmlAttributes(new { @class = "telerik-grid" })
)
Run Code Online (Sandbox Code Playgroud)

我的视图模型看起来像:

public class GrantApplicationListViewModel
{
     public int Id { get; set; }
     public string FirstName { get; set; }
     public string LastName { get; set; }
     public string FullName
     {
          get { return FirstName + " " + LastName; …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-2

4
推荐指数
1
解决办法
5526
查看次数

如何在ASP.NET MVC模型中为POST保存选定的DropDownList值?

我的模型中有一个属性,如下所示:

 public IList<SelectListItem> AllCards { get; set; }
Run Code Online (Sandbox Code Playgroud)

SelectListItem控制器使用数据库中的数据来填充属性.

另外,在我的视图中@Html.DropDownListFor(m=>m.AllCards,Model.AllCards,"--Select--").在POST视图期间出现问题.也就是说,onSubmit我Model.AllCards是空的,尽管在下拉列表中选择了一个值.我尝试将新属性添加到模型中,如下所示:

public SelectListItem SelectedCard
Run Code Online (Sandbox Code Playgroud)

然后像这样改变视图:

@Html.DropDownListFor(m=>m.AllCards,Model.SelectedCard,"--Select--")
Run Code Online (Sandbox Code Playgroud)

但它给了我一个错误,说转换不可能,就像那样.

我应该怎么做才能捕获我的dropdownlist值,以便在调用视图的POST版本后它可以在模型内部?谢谢

PS我知道有十个类似的问题,但没有一个有用.

asp.net asp.net-mvc razor asp.net-mvc-3 asp.net-mvc-4

4
推荐指数
1
解决办法
2万
查看次数

如何将自定义数据属性和类添加到`@ Html.EditorFor`?

我想为input生成的添加一些自定义属性@Html.EditorFor,我尝试了以下内容:

@Html.EditorFor(model => model.Percent, new { @class = "percent" })
Run Code Online (Sandbox Code Playgroud)

但它只是忽略了我的课程,从我可以通过搜索来看,该模板不支持添加自定义属性.

但是,如何创建自定义模板,添加对自定义属性的支持,同时保留旧模板的所有功能?

asp.net-mvc html-helper asp.net-mvc-4 razor-2

4
推荐指数
1
解决办法
2万
查看次数

使用Autofac注入接口的特定实例

我有一个控制器,它接收一个特定的接口实例.

界面看起来像这样:

public interface IMyInterface
{
   ... implementation goes here
}
Run Code Online (Sandbox Code Playgroud)

然后我有一些实现这个接口的类,如下所示:

public class MyClassA : IMyInterface
{
   ... implementation goes here
}

public class MyClassB : IMyInterface
{
   ... implementation goes here
}
Run Code Online (Sandbox Code Playgroud)

在我的ControllerA中,我有以下构造函数:

private ICustomerService customerService;
private IMyInterface myInterface;

puvlic ControllerA(ICustomerService customerService, IMyInterface myInterface)
{
   this.customerService = customerService;
   this.myInterface = myInterface;
}
Run Code Online (Sandbox Code Playgroud)

在我的global.ascx中:

protected void Application_Start()
{
   // Autofac
   var builder = new ContainerBuilder();
   builder.RegisterControllers(Assembly.GetExecutingAssembly());
   builder.RegisterType<NewsService>().As<INewsService>();

   IContainer container = builder.Build();
   DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
Run Code Online (Sandbox Code Playgroud)

我指定Autofac必须提供ICustomerService的实例.我如何指定IMyInterface的实例类型?在这种情况下,对于ControllerA,我希望Autofac注入一个ClassA实例.对于ControllerB,我希望它能够注入ClassB.我该怎么做?

更新2011-02-14:

让我告诉你我现实生活中的情况.我有一个NewsController,其构造函数如下所示:

public NewsController(INewsService …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection inversion-of-control autofac automapper

3
推荐指数
1
解决办法
4298
查看次数

如何对UrlHelper自定义帮助器方法进行单元测试

我正在使用ASP.NET MVC 3NUnit.我正在尝试编写一个单元来测试我的一个辅助方法.这里是:

public static class UrlHelperAssetExtensions
{
   private static readonly string yuiBuildPath = "http://yui.yahooapis.com/2.8.2r1/build/";

   public static string YuiResetFontsGridsStylesheet(this UrlHelper helper)
   {
      return helper.Content(yuiBuildPath + "reset-fonts-grids/reset-fonts-grids.css");
   }
}
Run Code Online (Sandbox Code Playgroud)

这是我的单元测试:

[Test]
public void YuiResetFontsGridsStylesheet_should_return_stylesheet()
{
   // Arrange
   RequestContext requestContext = new RequestContext();
   UrlHelper urlHelper = new UrlHelper(requestContext);

   // Act
   string actual = urlHelper.YuiResetFontsGridsStylesheet();

   // Assert
   string expected = yuiBuildPath + "reset-fonts-grids/reset-fonts-grids.css";
   Assert.AreEqual(expected, actual);
}
Run Code Online (Sandbox Code Playgroud)

我测试它的方法是否正确?当我在NUnit GUI中运行它时,我收到以下错误:

System.ArgumentNullException:值不能为null.参数名称:httpContext

这有可能测试吗?如果是这样,请明确说明如何获取httpContext的实例?

更新

我无法通过这项测试.在我的方法中,我有以下内容:

private static readonly string stylesheetPath = "~/Assets/Stylesheets/"; …
Run Code Online (Sandbox Code Playgroud)

asp.net asp.net-mvc nunit asp.net-mvc-3 asp.net-mvc-2

3
推荐指数
1
解决办法
3742
查看次数