我正在尝试使用MVC 3 Preview 1的新Razor视图引擎,并且真的想用NUnit/Moq编写一个简单的单元测试.我还没有看到任何实际的例子 - 尽管它是Razor真正的销售功能之一.
因此,如果我有一个使用DBConext对象的Controller(首先是EF4 CTP代码),并且视图根据控制器上调用的动作中加载的模型中提供的项目列表呈现下拉列表,我会喜欢能够测试该元素是否填充了其中的项目.
这是我的控制器:
public class WeatherReportController : Controller, IWeatherReportController
{
private IWeatherDb _weatherDb;
public WeatherReportController()
{
this._weatherDb = new WeatherDb();
}
public ActionResult Index()
{
WeatherReportIndexModel model = new WeatherReportIndexModel
{
Report = new WeatherReport {
Username = this.HttpContext.User.Identity.Name,
WeatherType = new WeatherType()
},
WeatherTypeList = _weatherDb.GetAllWeatherTypes()
};
return View(model);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的模型:
public class WeatherReportIndexModel
{
private IList<WeatherType> _weatherTypeList = new List<WeatherType>();
public IList<WeatherType> WeatherTypeList {
get
{
return _weatherTypeList;
}
set
{ …Run Code Online (Sandbox Code Playgroud) 我试图在表单发布之前允许多个文件上传.我想要的是用户只能看到一个文件上传元素,每次选择一个文件时,都会显示一个<li>包含文件名的新文件和一个图像/链接,用于从集合中删除该特定文件.有一个jQuery MultiFile插件可以满足我的需求,但我不能让自定义样式以我想要的方式工作,所以我自己动手.
到目前为止,我有以下代码.它成功添加<li>,使用新选择的文件隐藏文件上传元素,并将空文件上传元素附加到页面以供用户选择新文件.我正在努力适当地管理元素的删除,虽然它并不那么困难,但我一直盯着这一点,现在只是觉得我做错了.我希望其他人可能有一些见解,提示要清理它(即使它更优雅),等等.代码如下.
HTML:
<div id="product-image-area" class="group" style="border-bottom: none; padding-top: 0">
<ul id="photos" class="nobull">
<li id="no-product-images-msg" class="" >
<%= Html.Image("no-photos.png") %>
</li>
</ul>
</div>
<div id="upload-area">
<h4 class="st">Upload an image</h4>
<p class="note">Allowed file types are .gif, .jpg, .jpeg, and .png</p>
<p id="file-input" class="st sb"><input class="photo-upload" id="VenuePhotos_0" name="VenuePhotos[]" type="file" /></p>
</div>
Run Code Online (Sandbox Code Playgroud)
脚本:
$(function () {
$('.photo-upload').live('change', function () {
var fileCount = new Number($(this).parent().children('.photo-upload').length);
$('#photos').append('<li id="venue_photo_' + (fileCount - 1) + '">' + $(this).val() + '<img title="' …Run Code Online (Sandbox Code Playgroud) 我正在使用editortemplate,我在下面显示了部分代码.脚本标记针对部分视图呈现的每个行重复.
有没有办法将脚本标记放入页眉,或者至少只将它包含在页面中一次?
我想在局部视图中的代码,因为它是真正属于的地方.
@* DisplayTemplates/contact.cshtml *@
@model Online.Web.Contacts.Contact
<script id="xxx">
stuff here.
</script>
<tr>
<td>@Html.EditorFor(x => x.FirstName)</td>
</tr>
Run Code Online (Sandbox Code Playgroud) 我正在使用存根来更新我的实体,并且当更新的实体由值从非空值更改为空值的列时,空值不会持久保存到数据库,即记录继续保留先前的非空值.
我究竟做错了什么?
public void UpdateEntity(Entity e)
{
_context.Works.Attach(new Entity{ Id = e.Id });
_context.ApplyCurrentValues("Entities", e);
_context.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud) 我在Program和Student表之间有1:N关系,EF转换为导航属性.现在我要删除此导航学生中的所有记录.我开始是这样的:
foreach(Student student in program.Students)
program.Students.Remove(student);
Run Code Online (Sandbox Code Playgroud)
但我对此有点怀疑.
比我尝试过这样:
while (program.Students.Count > 0)
program.Students.Remove(program.Students.ToList()[0]);
Run Code Online (Sandbox Code Playgroud)
但这似乎也很奇怪.
是否有一些更简单的方法可以做到这一点,或者不是哪种方式最好?
通过jQuery ajax将字符串传递给控制器动作很简单,但是可以将一组变量序列化为对象,将其发送到控制器,并让控制器将其识别为对象吗?
例如:
在服务器中,您有一个Obj类:
class Obj{
string a; int b; double c;
}
Run Code Online (Sandbox Code Playgroud)
在控制器中,您有一个期望Obj对象的方法
public JsonResult UpdateObj(Obj obj){
//stuff
}
Run Code Online (Sandbox Code Playgroud)
在Jquery中是否有一种方法可以将一些JavaScript变量序列化为类Obj,然后通过AJAX帖子将其发送到MVC控制器动作?
我在MVC3中创建了局部视图.现在我想按下提交按钮发送文本框值作为表单提交的参数
我的部分观点是这样的
@using (Html.BeginForm("Searching", "AccountManager", FormMethod.Post, new { name ="Wat should i put here" }))
{
<input id="account" type="text" class="s" />
<input id="Search" type="submit" class="b" value="hi" />
}
Run Code Online (Sandbox Code Playgroud)
我的控制器就像
public viewResult Searching(string name)
{
// bussiness logic
return view();
}
Run Code Online (Sandbox Code Playgroud) 如何更改HtmlFieldPrefix的索引?
我是Children[0]从EditorFor()获得的,我想制作它Children[@Model.Id]
或者Children[2].Children[4]来自EditorFor()而我想制作它Children[@"ParentModel".Id].Children[@Model.Id]
直到运行时我才会知道实际的前缀.最好有内置的方法来改变它吗?
或者只是弄乱字符串?我还是C#字符串函数的新手.
一开始,我们有这个基本的枚举.
public enum E_Levels {
[ValueOfEnum("Low level")]
LOW,
[ValueOfEnum("Normal level")]
NORMAL,
[ValueOfEnum("High level")]
HIGH
}
Run Code Online (Sandbox Code Playgroud)
而且我想得到List<string> 任何一个枚举.这样的东西Extensions.GetValuesOfEnum<E_Levels>()可以返回List<string>"低级别","正常级别"和"高级别".
StackOF帮助我获得了一个值属性:
public static class Extensions {
public static string ToValueOfEnum(this Enum value) {
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
ValueOfEnum[] attribs = fieldInfo.GetCustomAttributes(typeof(ValueOfEnum), false) as ValueOfEnum[];
return attribs.Length > 0 ? attribs[0].value : null;
}
}
Run Code Online (Sandbox Code Playgroud)
无论枚举如何,我都可以调用这种方法:E_Levels.LOW.ToValueOfEnum().
此外,StackOF帮助我获得了List<string>一个特定的枚举.我在控制器中制作了这个方法:
private List<string> GetLevels() {
List<string> levelsToReturn = new List<string>();
var levels = Enum.GetValues(typeof(E_Levels)).Cast<E_Levels>(); …Run Code Online (Sandbox Code Playgroud) 我们生产的AppFabric Cache几乎每天都会崩溃,而且非常不稳定.记录以下错误:
Microsoft.ApplicationServer.Caching.DataCacheException:ErrorCode:SubStatus:存在临时故障.请稍后重试.(没有足够的辅助人员,或者他们处于受限状态.)
Microsoft.ApplicationServer.Caching.DataCacheException:ErrorCode:SubStatus:存在临时故障.请稍后重试.(请求没有找到主要的.)
AppFabric缓存服务崩溃.{外部商店租约已过期:Microsoft.Fabric.Federation.ExternalRingStateStoreException:Microsoft.Fabric.Federation.SiteNode.PerformExternalRingStateStoreOperations中的Microsoft.Fabric.Data.ExternalStoreAuthority.UpdateNode(NodeInfo nodeInfo,TimeSpan超时)租约已过期(Boolean&canFormRing,Boolean isInsert,Boolean isJoining)}
有人可以请我提供一些意见吗?这是一个启用HA的缓存环境,具有3个缓存主机.所有这些都在Windows Server 2008 Enterprise Edition上运行,SQL Server用于配置.
c# ×6
razor ×5
jquery ×2
.net ×1
appfabric ×1
asp.net-mvc ×1
enums ×1
file-upload ×1
generics ×1
nunit ×1
unit-testing ×1