嗨我正在尝试使用PartialViewResult方法在某些条件不是页面时重定向页面.但我无法做到.我的代码如下:
public PartialViewResult Visit()
{
int visit = 0;
if (base.IsLoggedIn() == true)
{
visit++;
}
else
{
// toDO redirect to home/index page
}
}
Run Code Online (Sandbox Code Playgroud) 我对编辑器模板有疑问.例如,我有一个带有日期字段,产品奖励字段和产品字段权重的模型.我已成功为DateTime和Decimal创建了编辑器模板.我理解当我在任何字段使用EditorFor时,它取决于字段的数据类型.
因此,当我使用:EditorFor(m=>m.DateOfBirth)这是DateTime格式时,它在EditorTemplates文件夹中查找DateTime模板,并且EditorFor(m=>m.ProductPrice)它是double,它查找Double模板等.)
问题是我如何区别我想使用一个带有##0.00(double)ProductPrize的字符串格式的Decimal模板和另一个带有##0.0000(double)ProductWeight 格式的模板?
任何帮助,将不胜感激
我试图用所选值填充下拉值.我提到了相关的文章,我也在做同样的事情.Still Dropdown未填充所选值.不确定我错过了什么.
控制器代码:
var RegionId = new[] {
new RegionKeyValues { RegionId = 1, RegionValue = "A" },
new RegionKeyValues { RegionId = 2, RegionValue = "B" },
new RegionKeyValues { RegionId = 3, RegionValue = "D" }
};
ViewData["RegionId"] = new SelectList(RegionId, "RegionId", "RegionValue", 1);
Run Code Online (Sandbox Code Playgroud)
查看代码:
<%= Html.DropDownList("RegionId",
(SelectList)ViewData["RegionId"],
"-- Select --")%>
Run Code Online (Sandbox Code Playgroud) 我正在学习ASP MVC,因此是TDD和MVC框架的新手.我在编写模拟TestMethod以测试Controller内部的一个Action时发现了一个问题,它的主体是"RedirectToAction".
编写testMethod以确保重定向的Action正确无误.我读过它可以检查RedirectToRouteResult.RouteValues,并获得"action"键的值.即使在调试(观察属性),我也能够正确.但@编译时测试显示异常Cannot apply Indexing with [] to an expression of type RedirectToRouteResult.RouteValues.
以下是参考的代码段:
public void TestHomeControllerContactRedirectRoute()
{
HomeController controller = new HomeController();
ContactMessage message=new ContactMessage(){
Email="abc@xyz.com",
MessageBody="Some message here!!",
Name="Sun"
};
RedirectToRouteResult resultRoute = controller.Contact(message) as RedirectToRouteResult;
if (resultRoute != null)
{
Assert.AreEqual("Contact", resultRoute.RouteValues["action"]);
}
}
Run Code Online (Sandbox Code Playgroud) 嗨,大家怎么能得到选定的价值?
这是我的模特
public class PaymentModels
{
public int CreditCardNumber { get; set; }
public List<SelectListItem> CardType { get; set; }
public static List<SelectListItem> getCardTypes()
{
List<SelectListItem> cardType = new List<SelectListItem>();
cardType.Add(new SelectListItem { Text = "American Express"});
cardType.Add(new SelectListItem { Text = "Mastercard" });
cardType.Add(new SelectListItem { Text = "Visa" });
return cardType;
}
}
Run Code Online (Sandbox Code Playgroud)
这是通过控制器在视图上显示列表的方式
ViewData["List"] = PaymentModels.getCardTypes();
Run Code Online (Sandbox Code Playgroud)
这是我的回发方法
[HttpPost]
public ActionResult Payment(PaymentModels pay)
{
String vr;
foreach(var v in pay.CardType) {
vr= v.Selected.ToString();
}
return View();
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,当调用post事件时,如何从我的cardtype列表中获取所选值?
MVC 2
我正在尝试使用jQuery发布到MVC操作,但我得到一个异常,声明它id是null.我是否需要在控制器操作上使用某种属性来接受像这样的json数据包?我还缺少什么?
[HttpPost]
public ActionResult SearchCategory(int id, string SearchTerm, int PageNumber, int SiteIdFilter)
{
return Json(AssociationsDao.SearchCategory(id, SearchTerm, PageNumber, SiteIdFilter));
}
Run Code Online (Sandbox Code Playgroud)
post('<%= Url.Action("SearchCategory") %>',
JSON.stringify({id: 12, SearchTerm: '', PageNumber: 1, SiteIdFilter: 1}),
function(d) { alert(d); });
function post(targetURL, dataInput, success) {
$.ajax({
url: targetURL,
type: "POST",
contentType: "application/json; charset=utf-8",
data: dataInput,
dataType: "json",
success: success,
async: true
});
}
Run Code Online (Sandbox Code Playgroud)
从Chrome开发人员工具中,这是一个例外:
参数字典包含非可空类型'System.Int32'的参数'id'的空条目,用于'Current.Web中的方法'System.Web.Mvc.ActionResult SearchCategory(Int32,System.String,Int32,Int32)' .BackOffice.WebUI.Controllers.SiteProductAssociationsController".可选参数必须是引用类型,可空类型,或者声明为可选参数.参数名称:参数
编辑
这是来自chrome的帖子数据的屏幕截图; 我看不出有什么问题:

我一直在使用Action获取报告的数据,作为JSON对象,使用jQuery的ajax将表单发布到它上面没有问题.
但是现在我需要根据参数的值返回不同的结果类型.它应该返回JSON,Excel文件(使用HTML构建)或PDF文件.所以我在我的控制器类上创建了一个嵌套枚举,用于分隔可用的返回类型.
但是现在,当我尝试从URL调用Action来生成文件时,它会抛出一条ArgumentException消息:
The parameters dictionary contains a null entry for parameter 'dataInicio' of non-nullable type 'System.DateTime' for method 'System.Web.Mvc.ActionResult DadosRelatorioResumoLancamentos(System.Nullable`1[System.Int32], System.String, System.DateTime, System.DateTime, TipoResultado)' in 'Imovelweb.Web.Intranet.Controllers.RelatoriosController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
仍然,dataInicio参数出现在查询字符串中:
我已经尝试了两种方法的原始请求(返回JSON内容),它适用于POST,但不适用于GET(ArgumentException抛出相同的内容).
我错过了什么?
这是Action方法的签名:
public ActionResult DadosRelatorioResumoLancamentos(
int? codFantasia,
string numAp,
DateTime dataInicio,
DateTime dataFim,
TipoResultado tipoResultado = TipoResultado.Json
);
Run Code Online (Sandbox Code Playgroud)
以下是枚举:
public enum TipoResultado
{ …Run Code Online (Sandbox Code Playgroud) 我正在使用ASP.NET MVC2.
我喜欢如何注释我的模型的字段并检查ModelState.IsValid.
检查它的明显原因是在进行验证时,但是如果将新的验证规则应用于模型,是否应该始终检查它?
这样你就不会忘记/需要检查控制器是否检查IsValid.
在所有情况下都有一些理由不进行此检查吗?也许只是当控制器动作有副作用(DB写入等)?
我有以下内容:
这个动作:
public virtual ActionResult Search(string search, string sort)
{
...
}
Run Code Online (Sandbox Code Playgroud)
使用空查询字符串参数从此URL调用:
http://myurl.com/mycontroller/search?search=&sort=
Run Code Online (Sandbox Code Playgroud)
现在我的理解是,从MVC 2开始,DefaultModelBinder会将这些值保留为空值.但是,我发现它们实际上是设置为空字符串.这实际上是预期的行为吗?这记录在哪里?
谢谢
asp.net asp.net-mvc defaultmodelbinder asp.net-mvc-3 asp.net-mvc-2
我在c#MVC2项目中有以下代码行:
string.Format(@"{0\:HH\:mm}", new TimeSpan(0))
Run Code Online (Sandbox Code Playgroud)
此行导致以下异常:
System.FormatException: Input string was not in a correct format.
Run Code Online (Sandbox Code Playgroud)
谁能告诉我为什么?我正在使用C#,asp.net,mvc2和.net framework 4
asp.net-mvc-2 ×10
asp.net-mvc ×8
c# ×5
asp.net ×2
.net ×1
.net-4.0 ×1
timespan ×1
unit-testing ×1