假设您有一个基本Employee类:
class Employee
{
public string Name;
public int Years;
public string Department;
}
Run Code Online (Sandbox Code Playgroud)
然后(在一个单独的类中)我有以下代码片段(我想我理解除了最后一个):
我相信下面的代码片段是有效的,因为数组initiliser创建了一个Employee对象数组,它们与分配给的workforce变量的类型相同.
Employee[] workforceOne = new Employee[] {
new Employee() { Name = "David", Years = 0, Department = "software" },
new Employee() { Name = "Dexter", Years = 3, Department = "software" },
new Employee() { Name = "Paul", Years = 4, Department = "software" } };
Run Code Online (Sandbox Code Playgroud)
然后我有以下代码片段.我相信这是有效的,因为Employee对象数组的含义是实现的Array()类的实现IEnumerable.因此,我相信这就是为什么数组可以分配给IEnumerable?
IEnumerable workforceTwo = new Employee[] {
new Employee() { Name = …Run Code Online (Sandbox Code Playgroud) 我在使用ValidationSummary(true)显示模型级错误时遇到了一些问题.如果ModelState不包含模型错误(即ModelState.AddModelError("", "Error Description"))但包含属性错误(使用数据注释添加),则会显示没有错误信息的验证摘要(当您查看源时).因此我的css显示一个空的红色框,如下所示:

如果没有属性错误,则不显示验证摘要.有了ValidationSummary(true)我会期待它只能显示验证错误,如果有模型误差.我误解了什么?
我有一个基本项目如下:
控制器:
public class HomeController : Controller
{
public ViewResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(IndexViewModel model)
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
模型:
public class IndexViewModel
{
[Required]
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
视图:
@model IndexViewModel
@Html.ValidationSummary(true)
@using(@Html.BeginForm())
{
@Html.TextBoxFor(m => m.Name)
<input type="submit" value="submit" />
}
Run Code Online (Sandbox Code Playgroud) 我有一个MVC3视图模型定义为:
[Validator(typeof(AccountsValidator))]
public class AccountViewModel
{
public List<string> Accounts { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
使用FluentValidation(v3.3.1.0)定义验证为:
public class AccountsValidator : AbstractValidator<AccountViewModel>
{
public AccountsValidator()
{
RuleFor(x => x.Accounts).SetCollectionValidator(new AccountValidator()); //This won't work
}
}
Run Code Online (Sandbox Code Playgroud)
并且可能会定义帐户验证:
public class AccountValidator : AbstractValidator<string> {
public OrderValidator() {
RuleFor(x => x).NotNull();
//any other validation here
}
}
Run Code Online (Sandbox Code Playgroud)
我希望列表中的每个帐户都按照文档中的描述进行修改.但是,调用SetCollectionValidator不起作用,因为在使用a时这不是一个选项,List<string>尽管如果定义为选项将存在List<Account>.我错过了什么吗?我可以改变我的模型使用List<Account>然后定义一个Account类,但我真的不想改变我的模型以适应验证.
作为参考,这是我正在使用的视图:
@model MvcApplication9.Models.AccountViewModel
@using (Html.BeginForm())
{
@*The first account number is a required field.*@
<li>Account number* @Html.EditorFor(m …Run Code Online (Sandbox Code Playgroud) 我想知道是否可以通过 ViewBag 传递模型对象。我尝试了以下代码,但不知何故在我的视图中,它只是显示的模型路径。
控制器:
public ActionResult Tempo()
{
DateTime date1 = new DateTime(1990, 1, 1);
Employee emp = new Employee(3, "fara", "hass", date1, 56.6m, 0);
ViewBag.EmployeeId = emp.EmployeeID;
ViewBag.Fname = emp.Fname;
ViewBag.Employee = emp;
}
Run Code Online (Sandbox Code Playgroud)
看法:
@{
ViewBag.Title = "Tempo";
}
@model LayoutProject.Models.Employee
<h2>Tempo</h2>
<div>
<h4>ViewBag</h4>
<br />
EmployeeID: @ViewBag.EmployeeId
<br />
Fname: @ViewBag.Fname
<br />
Employee : @ViewBag.Employee
</div>
Run Code Online (Sandbox Code Playgroud)
Fname 和 EmployeeId 正确显示,但不是 Employee 本身。我在这里遗漏了什么还是根本不可能通过 ViewBag 传递模型?
我试图了解(显示)模块模式中public属性的工作方式。卡尔·丹利(Carl Danley)提出的“ 显露模块模式 ”的优点是:
明确定义的公共方法和变量,可提高可读性
让我们看一下这段代码(fiddle):
var a = function() {
var _private = null;
var _public = null;
function init() {
_private = 'private';
_public = 'public';
}
function getPrivate() {
return _private;
}
return {
_public : _public,
init : init,
getPrivate : getPrivate,
}
}();
a.init();
console.log( a._public ); // null
console.log( a.getPrivate() ); // "private"
Run Code Online (Sandbox Code Playgroud)
null调用时返回a._public。我现在可以操纵该公共财产,例如a._public = 'public';。但是我不能从对象内部更改它。或至少这些更改没有通过。我有点期待它"public"是由init-method之前更新的。 …
我有以下代码来向用户显示帐号列表.
查看型号:
有时列表将为null,因为没有要显示的帐户.
public class AccountsViewModel
{
public List<string> Accounts { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
视图:
@model AccountsViewModel
using (@Html.BeginForm())
{
<ul>
@*if there are accounts in the account list*@
@if (Model.Accounts != null)
{
foreach (string account in Model.Accounts)
{
<li>Account number* <input type="text" name="account" value="@account"/></li>
}
}
@*display an additional blank text field for the user to add an additional account number*@
<li>Account number* <input type="text" name="account"/></li>
</ul>
...
}
Run Code Online (Sandbox Code Playgroud)
一切都编译得很好但是当我运行页面时,我得到一个NullReferenceException was unhandled在线:
@if (Model.Accounts != …Run Code Online (Sandbox Code Playgroud) 我有一个简单的代码(见下文)我从一本书中复制过来.但我对这条线路有几个问题<Grid TextBlock.FontSize="48">.
从我收集的内容来看,TextBlock.FontSize是一个附加属性,但我最初认为附加属性是为了引用父对象(即当Grid.Row附加属性引用父Grid元素时).但是从这里使用它可能是我的理解不正确?这是一个附加属性,如果可以,它可以用于子元素?
其次,在网格上设置TextBlock.FontSize.但是,我没有在xaml中使用TextBlock元素(我知道).我只使用了内容定义的按钮.但是,如果我将TextBlock.FontSize更改为其他值,则字体大小会更改.因此,如何使用TextBlock.FontSize?TextBlock在哪里?
先感谢您.
<Window x:Class="UseAGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid TextBlock.FontSize="48">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="250" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Grid.RowSpan="2"
Content="2 Rows" />
<GridSplitter Grid.Row="0"
Grid.RowSpan="2"
Grid.Column="1"
Width="1"
Background="Green"
ResizeBehavior="PreviousAndNext"
ResizeDirection="Columns" />
<Button Grid.Column="2"
Grid.ColumnSpan="2"
Content="2 Columns" />
<Button Grid.Row="1"
Grid.Column="2"
Content="1,2" />
<Button Grid.Row="1"
Grid.Column="3"
Content="1,3" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud) 这个让我发疯,在我失去理智之前请帮忙.
问题摘要:我的模型"Thread"有"ForumMessage"集合,每个ForumMessage都有一个Multi select下拉列表.我想要做的就是根据来自数据库的值设置所选值.我已经通过很多线程,但无法找到解决方案.
如果您知道任何此类问题,请告诉我,我会通过他们.
以下是我的模特
public class Thread
{
public List<ForumMessage> Messages { get; set; }
//Master list coming from DB
public List<Classifications> AllClassifications { get; set; }
public string Subject { get; set; }
public int[] ThreadSelectedClassifications { get; set; }
}
public class ForumMessage
{
public string MessageName { get; set; }
public int[] SelectedClassifications { get; set; }
}
public class Classifications
{
public int ID { get; set; }
public string Title { get; set; } …Run Code Online (Sandbox Code Playgroud) c# html.dropdownlistfor .net-4.5 html.listboxfor asp.net-mvc-5
我有一个名为_Layout.cshtml并放置在文件夹中的母版页Views/Shared/_Layout.cshtml。我创建了一个控制器:SharedController.cs将数据传递给_Layout.cshtml
但不进入控制器。如何将数据传递到每个加载母版页_Layout.cshtml?
这是控制器,例如:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace HAnnoZero.Controllers
{
public class SharedController : Controller
{
public ActionResult _Layout()
{
ViewBag.help = "ciao";
return View();
}
}
}
Run Code Online (Sandbox Code Playgroud) c# ×5
.net-4.5 ×1
arrays ×1
asp.net-mvc ×1
casting ×1
generics ×1
javascript ×1
razor ×1
viewbag ×1
wpf ×1