假设您有一个ASP.NET MVC项目并且正在使用服务层,例如在asp.net站点上的这个联系管理器教程中:http: //www.asp.net/mvc/tutorials/iteration-4-make-的应用程序,松散耦合-CS
如果您有视图的视图模型,那么服务层是否适合提供每个视图模型?例如,在服务层代码示例中有一种方法
public IEnumerable<Contact> ListContacts()
{
return _repository.ListContacts();
}
Run Code Online (Sandbox Code Playgroud)
如果你想要一个IEnumerable,它应该进入服务层,还是在其他地方是"正确"的地方?
也许更合适的是,如果您为与ContactController关联的每个视图都有一个单独的视图模型,那么ContactManagerService是否应该有一个单独的方法来返回每个视图模型?如果服务层不是正确的位置,那么viewmodel对象应该在哪里被初始化以供控制器使用?
我想获取在MVC用户表单中输入的数据并将其显示在不同的视图中.
该类具有以下私有变量:
IList<string> _pagecontent = new List<string>();
Run Code Online (Sandbox Code Playgroud)
以下操作接受FormCollection对象,验证它,并将其作为List传递到"预览"视图:
[Authorize(Roles = "Admins")]
[ValidateInput(false)]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UpdateContent(FormCollection collection)
{
if (ModelState.IsValid)
{
string PageToInsert = collection["PageToInsert"];
string PageHeader = collection["PageHeader"];
string PageBody = collection["PageBody"];
//validate, excluded...
_pagecontent.Add(PageToInsert);
_pagecontent.Add(PageHeader);
_pagecontent.Add(PageBody);
}
return RedirectToAction("Preview", _pagecontent);
}
Run Code Online (Sandbox Code Playgroud)
预览视图具有以下页面指令,用于传递强类型对象List:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Template.Master" Inherits="System.Web.Mvc.ViewPage<List<string>>" %>
Run Code Online (Sandbox Code Playgroud)
我希望能够使用Model对象来获取我的数据,但我不能.在接下来的行中,我得到一个error index out of bounds异常,声明索引必须是非负数且小于集合的大小:
<% if (Model[0].ToString() == "0") { %>
Run Code Online (Sandbox Code Playgroud)
并且一些奇怪的参数已经添加到URL中,因为它解析为
http://localhost:1894/Admin/Preview?Capacity=4&Count=3
所以我有两个问题:
我使用自动映射器来映射多个对象(db类到ui对象).
地图1:
Mapper.CreateMap<sourceone, destination>().ForMember(sss => sss.one, m => m.MapFrom(source => source.abc));
Run Code Online (Sandbox Code Playgroud)
地图2:
Mapper.CreateMap<sourcetwo, destination>().ForMember(sss => sss.two, m => m.MapFrom(source => source.xyz));
destination d = new destination();
Run Code Online (Sandbox Code Playgroud)
//地图1
d = AutoMapper.Mapper.Map<sourceone, destination>(sourceone);
Run Code Online (Sandbox Code Playgroud)
//地图2
d = AutoMapper.Mapper.Map<sourcetwo, destination>(sourcetwo);
Run Code Online (Sandbox Code Playgroud)
一旦我调用'Map 2',使用Map 1填充的值就会丢失..(即destination.one变空).我该如何解决?
Razor视图在表单中有3个按钮.所有按钮的操作都需要表单值,这些值基本上是输入字段的值.
每次我点击任何按钮时,它都会将我重定向到默认操作.您能否指导我如何根据按钮提交表单到不同的操作?
我非常感谢您的时间,指导和帮助.
我在这里遗漏了一些东西.我有这个jQuery JavaScript:
$.ajax({
type: "POST",
url: "/update-note-order",
dataType: "json",
data: {
orderedIds: orderedIds,
unixTimeMs: new Date().getTime()
}
});
Run Code Online (Sandbox Code Playgroud)
orderedIdsJavaScript编号数组在哪里(例如var orderedIds = [1, 2]).
处理Controller方法是:
[HttpPost]
public void UpdateNoteOrder(long[] orderedIds, long unixTimeMs)
{
...
}
Run Code Online (Sandbox Code Playgroud)
当我放入Debugger.Break()时UpdateNoteOrder(),orderedIds是null在Watch窗口中.(unixTimeMs但是,有一个数值.)
如何通过数字阵列$.ajax(),从而orderedIds是long[]在我的控制?
我正在运行MVC3和Windows身份验证Web应用程序.当我部署到IIS6时,它运行良好,直到我点击需要身份验证的页面.然后,当我在我的应用程序中没有跟踪它并且我的web.config配置为windows auth时,它会自动重定向到/ Account/Login.
有任何想法吗?
这是我的整个web.config文件:http://pastie.org/1568510
asp.net-mvc iis-6 windows-authentication sharp-architecture asp.net-mvc-3
我是C#.net MVC的新手,我正在尝试添加FullCalendar到MVC应用程序.
该FullCalendar脚本自动添加?start={}&end={}到URL ...这很好,但我不知道如何querystring在控制器中使用变量.
我在Webforms应用程序中使用的方法不起作用.我需要添加到控制器以访问querystring变量?
我目前正在考虑在我自己的MVC Web框架中使用Reflection类(主要是ReflectionClass和ReflectionMethod),因为我需要自动实例化控制器类并调用他们的方法而不需要任何必需的配置("约定优于配置"方法).
我担心性能,即使我认为数据库请求可能是比实际PHP代码更大的瓶颈.
所以,我想知道从性能的角度来看,是否有人对PHP 5 Reflection有任何好的或坏的体验.
此外,我很想知道任何一个流行的PHP框架(CI,Cake,Symfony等)是否实际使用Reflection.
如何从父类调用子类的函数?考虑一下:
class whale
{
function __construct()
{
// some code here
}
function myfunc()
{
// how do i call the "test" function of fish class here??
}
}
class fish extends whale
{
function __construct()
{
parent::construct();
}
function test()
{
echo "So you managed to call me !!";
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用jquery对asp.net mvc控制器动作进行ajax调用:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult GetWeek(string startDay)
{
var daysOfWeek = CompanyUtility.GetWeek(User.Company.Id, startDay);
return Json(daysOfWeek);
}
Run Code Online (Sandbox Code Playgroud)
当会话超时时,此调用将失败,因为User对象存储在会话中.我创建了一个自定义authorize属性,以检查会话是否丢失并重定向到登录页面.这适用于页面请求,但它不适用于ajax请求,因为您无法从ajax请求重定向:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class AuthorizeUserAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (!httpContext.Request.IsAjaxRequest())
{//validate http request.
if (!httpContext.Request.IsAuthenticated
|| httpContext.Session["User"] == null)
{
FormsAuthentication.SignOut();
httpContext.Response.Redirect("~/?returnurl=" + httpContext.Request.Url.ToString());
return false;
}
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
我在另一个线程上读到,当用户未经过身份验证并且你发出ajax请求时,你应该将状态代码设置为401(未授权),然后在js中检查它并将它们重定向到登录页面.但是,我不能让这个工作:
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (Request.IsAjaxRequest() && (!Request.IsAuthenticated || User == null))
{
filterContext.RequestContext.HttpContext.Response.StatusCode = 401;
}
else
{
base.OnActionExecuting(filterContext);
} …Run Code Online (Sandbox Code Playgroud) asp.net-mvc ×8
c# ×4
jquery ×2
php ×2
automapper ×1
http-post ×1
iis-6 ×1
inheritance ×1
performance ×1
query-string ×1
razor ×1
reflection ×1