以下是EverythingController中的操作方法MovieCustomer的粘贴。 Viewmodel用于组合两种模型:客户和电影,并通过ApplicationDbContext(_context)从数据库填充信息。
当有MovieId和CustomerId的值时,Routeing成功工作并呈现页面
例如/ Everything / MovieCustomer / 1/1
如果一个或两个值都为空,我希望页面也可以加载。 到目前为止,这两个int参数都是可以为空的,并且如果两个参数都为空,那么方法中会有一个if语句将参数更改为1。 到目前为止,如果值为空,浏览器将返回404错误。
当其中一个或其中一个参数为空时,如何让页面起作用? 谢谢
[Route("Everything/MovieCustomer/{movieId}/{customerId}")] public ActionResult MovieCustomer(int? movieId, int? customerId) { var viewmodel = new ComboViewModel { _Customers = new List<Customer>(), _Movies = new List<Movies>(), _customer = new Customer(), _movie = new Movies() }; viewmodel._Customers = _context.Customers.ToList(); viewmodel._Movies = _context.Movies.ToList(); if (!movieId.HasValue) movieId = 1; if (!customerId.HasValue) customerId = 1; viewmodel._customer = viewmodel._Customers.SingleOrDefault(a => a.Id == customerId); viewmodel._movie = viewmodel._Movies.SingleOrDefault(a => a.Id == movieId); return View(viewmodel); }Here is a paste of the action method MovieCustomer in the EverythingController. The Viewmodel is used to Combine two Models: Customer & Movies, and is populated with information from the Database via the ApplicationDbContext (_context).
The Routeing works successfully and renders the page when there are values for MovieId and CustomerId
e.g. /Everything/MovieCustomer/1/1
I want the page to also load if one or both of the values are null. So far both of the int parameters were made nullable and there is an if statement in the method to change the parameters to 1 if either is null. So far if the values are null the browser returns a 404 error.
How can I get the page to function when one or either of the parameters are null? Thanks
[Route("Everything/MovieCustomer/{movieId}/{customerId}")] public ActionResult MovieCustomer(int? movieId, int? customerId) { var viewmodel = new ComboViewModel { _Customers = new List<Customer>(), _Movies = new List<Movies>(), _customer = new Customer(), _movie = new Movies() }; viewmodel._Customers = _context.Customers.ToList(); viewmodel._Movies = _context.Movies.ToList(); if (!movieId.HasValue) movieId = 1; if (!customerId.HasValue) customerId = 1; viewmodel._customer = viewmodel._Customers.SingleOrDefault(a => a.Id == customerId); viewmodel._movie = viewmodel._Movies.SingleOrDefault(a => a.Id == movieId); return View(viewmodel); }最满意答案
您可以使用单独的路线实现此目的,或将参数更改为可选。
使用3个属性时,为每个选项添加单独的路由 - 未指定参数时,仅指定movieId时,以及指定全部3个参数时。
[Route("Everything/MovieCustomer/")] [Route("Everything/MovieCustomer/{movieId}")] [Route("Everything/MovieCustomer/{movieId}/{customerId}")] public ActionResult MovieCustomer(int? movieId, int? customerId) { // the rest of the code }或者,您可以将您的路线参数更改为可选路线(通过在路线定义中添加? ),这应该涵盖您拥有的所有3种情况:
[Route("Everything/MovieCustomer/{movieId?}/{customerId?}")] public ActionResult MovieCustomer(int? movieId, int? customerId) { // the rest of the code }请记住,这两个示例都不支持仅提供customerId 。
You can achieve this using separate routes, or change your parameters to be optional.
When using 3 attributes, you add separate routes for each of the options that you have - when no parameters are specified, when only movieId is specified, and when all 3 parameters are specified.
[Route("Everything/MovieCustomer/")] [Route("Everything/MovieCustomer/{movieId}")] [Route("Everything/MovieCustomer/{movieId}/{customerId}")] public ActionResult MovieCustomer(int? movieId, int? customerId) { // the rest of the code }Alternatively you an combine change your route parameters to optional (by adding ? in route definition) and this should cover all 3 cases that you have:
[Route("Everything/MovieCustomer/{movieId?}/{customerId?}")] public ActionResult MovieCustomer(int? movieId, int? customerId) { // the rest of the code }Keep in mind that neither sample supports the case where you provide only customerId.
更多推荐
发布评论