我有一个视图,它内部有部分视图渲染:
<div class="partialViewDiv">
@Html.RenderPartial("partial", Model.SomeModelProperty);
</div>
Run Code Online (Sandbox Code Playgroud)
还有一个控制器,它返回这个视图
public ActionResult Action()
{
...
var model = new SomeModel(){SomeModelProperty = "SomeValue"}
return View("view", model);
}
Run Code Online (Sandbox Code Playgroud)
如何测试视图呈现我知道:
[TestMethod]
public void TestView()
{
...
var result = controller.Action();
// Assert
result.AssertViewRendered().ForView("view").WithViewData<SomeModel>();
}
Run Code Online (Sandbox Code Playgroud)
但是当我打电话的时候
result.AssertPartialViewRendered().ForView("partial").WithViewData<SomeModelPropertyType>();
Run Code Online (Sandbox Code Playgroud)
我收到此错误消息
Expected result to be of type PartialViewResult. It is actually of type ViewResult.
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
是否有一个单行(可能来自Guava或Apache Collections)从一组中获取子列表.在内部它应该做这样的事情:
public <T> List<T> sublist(Set<T> set, int count) {
Iterator<T> iterator = set.iterator();
List<T> sublist = new LinkedList<T>();
int pos = 0;
while (iterator.hasNext() && pos++ < count) {
sublist.add(iterator.next());
}
return sublist;
}
Run Code Online (Sandbox Code Playgroud)
显然,如果没有足够的元素,它必须尽可能多地返回.
好吧,我有这样简单的div结构:
<div id="wrapper">
<div id="1" class="block">
1
</div>
<div id="2" class="block">
2
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
#1可以通过javascript动态更改内容,并且可以根据内容更改其高度.我想要的是做#2同样的高度#1.我知道我可以使用javascript来达到这个目的但是我建议必须有一些不那么棘手的方法来使这些div的高度仅使用CSS和/或改变divs的位置如何.
这项任务很奇怪.我必须创建html表但是我不允许使用传统<table>标签.我的表应该是这样的:
这样做很容易,如下所示:
<table>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="5"></td>
</tr>
...
Run Code Online (Sandbox Code Playgroud)
但是,正如我所说,我不能用传统的表格标签(table,tr,td,th).这是我目前拥有的JSFIddle.我怎样才能获得与之相同的结果,<td colspan="5"></td> 但只使用div和CSS.
编辑:
*表格单元格在一行中的宽度不能固定,它应该是动态的,应该可以使它们(单元格)的宽度不同(在一行中).
*表格单元格在同一列的不同行中的宽度必须相等.就像传统的桌子一样.只有"colspanned"单元格的宽度必须不同.
我想编写自定义验证属性并向验证结果添加具有验证错误的其他成员名称。问题是我想根据属性名称和无效的匹配属性索引或键(我想验证 IEnumerables 或 IDictionaries)动态生成成员名称,例如Names[0]、等Names[1]。Names[key]例如:
模型:
public class ModelClass
{
[ItemMaxLength(10)]
[Display(ResourceType = typeof(CategoriesRename), Name = "CategoryNamesFieldName")]
public IDictionary<string, string> Names { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
属性:
public class ItemMaxLengthAttribute : ValidationAttribute
{
private readonly int _maxLength = int.MaxValue;
public ItemMaxLengthAttribute(int maxLength)
{
_maxLength = maxLength;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
...
// I can get instance and it's type from validation context
var instance = validationContext.ObjectInstance; // which is instance …Run Code Online (Sandbox Code Playgroud) 我有很多不同类型的文件(包括二进制文件)的存储库.问题是我决定重构我的存储库并将大部分文件(大约是5G)移动到另一个文件夹(在同一个存储库中).所以,当然,我想使用find renames工具,Tortoise Hg但由于很多文件应用程序客户端冻结,并在一段时间后停止响应.我知道那些文件只是被移动了,所以如何在不使用工具的情况下手动强制TortoiseHg将它们标记为重命名?
PS当然我可以使用这个工具几次只用于所有文件的一部分,但我想知道是否有可能没有找到重命名工具.
我一直在使用TempData很长时间,并且面临着一些奇怪的问题.我有基本情景:
[HttpPost]
public ActionResult Create(ProductCreateModel newProduct)
{
// create and save product to db
// try upload product to external site
try { UploadProductToEbay(newProduct); }
catch {
TempData["error"] = "error";
return RedirectToAction("Edit", newProduct.Id);
}
...
}
[HttpGet]
public ActionResult Edit(int Id)
{
var error = TempData["error"]; // at this point temp data collection is empty and have no idea why
...
}
Run Code Online (Sandbox Code Playgroud)
上传失败并return RedirectToAction("Edit", newProduct.Id);执行行时会发生此问题.丢失临时数据值的原因可能不是很明显?
更新: 当我使用时
TempData["error"] = "error";
RedirectToAction(...);
Run Code Online (Sandbox Code Playgroud)
在catch块之外一切正常,临时数据值转移到Edit动作.
嗯...问题更复杂,然后问题标题说.首先,我有一个HtmlHelper的扩展方法,它根据当前的路由参数生成带参数的html链接.也就是说,如果我在页面上.../page?param1=val1¶m2=val2,当我调用我的方法ActionQuryLink来生成链接时,@Html.ActionQuryLink("link text", "action", new { param3 = "value3" })我会得到一个链接<a href=".../page?param1=val1¶m2=val2¶m3=value3">link text</a>.好吧,扩展类本身是:
public static class ActionLinkHelper
{
public static MvcHtmlString ActionQueryLink(this HtmlHelper htmlHelper, string linkText, string action)
{
return (ActionQueryLink(htmlHelper, linkText, action, null, null));
}
public static MvcHtmlString ActionQueryLink(this HtmlHelper htmlHelper, string linkText, string action, object routeValues)
{
/*line 16*/return (ActionQueryLink(htmlHelper, linkText, action, routeValues, null));
}
public static MvcHtmlString ActionQueryLink(this HtmlHelper htmlHelper, string linkText, string action, object routeValues, IDictionary<string, object> htmlAttributes)
{
var …Run Code Online (Sandbox Code Playgroud) 我有一个家庭控制器
public class HomeController : Controller
{
public ActionResult Index()
{
var m = new ModelClass
{
Prop1 = 1,
Prop2 = "property 2"
};
return View(m);
}
public ActionResult SubAction()
{
ModelState.AddModelError("key", "error message value");
return RedirectToAction("Index");
}
}
Run Code Online (Sandbox Code Playgroud)
模型:
public class ModelClass
{
public int Prop1 { get; set; }
public string Prop2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
并查看:
@model MvcApplication9.Models.ModelClass
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@Html.TextBoxFor(m => m.Prop1)
@Html.TextBoxFor(m => m.Prop2)
@Html.ValidationMessage("key")
<br/>
@Html.ActionLink("action", "SubAction", "Home") …Run Code Online (Sandbox Code Playgroud) 好吧,我有简单的ajax形式:
这是 MyPartialView
@using(Ajax.BeginForm("action", "controller", new AjaxOptions
{
OnBegin = "beginRequest",
OnComplete = "completeRequest",
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "div-to-replace"
}, }))
{
<input type="text" id="my-input" />
...
}
Run Code Online (Sandbox Code Playgroud)
这是父视图:
<div id="div-to-replace">
@Html.RenderPartial("MyPartialView")
</div>
Run Code Online (Sandbox Code Playgroud)
在我的控制器中我有:
[HttpPost]
public ActionResult action(Model model)
{
if (ModelState.IsValid)
{
// do staff with model
// return partial view
return PartialView("MyPartialView");
}
// else add error and return json result
return Json(new {error = "invalid data"});
}
Run Code Online (Sandbox Code Playgroud)
和我在ajax上的javascript完整方法:
function completeRequest(data) {
var result …Run Code Online (Sandbox Code Playgroud) c# ×6
asp.net-mvc ×4
css ×2
html ×2
unit-testing ×2
css-tables ×1
guava ×1
height ×1
html-helper ×1
java ×1
jquery ×1
json ×1
mercurial ×1
modelstate ×1
moq ×1
properties ×1
reflection ×1
tempdata ×1
tortoisehg ×1
validation ×1
view ×1