[英]url not working in MVC with parameters
I am developing a project in MVC
and it has been working all right, but I have not been able to solve an error, I try to access a url that creates from global. asax
我正在
MVC
开发一个项目,它一直可以正常工作,但是我无法解决错误,我尝试访问从global. asax
创建的URL global. asax
global. asax
with parameters but it gives me error code 404. 带参数的
global. asax
,但它给我错误代码404。
routes.MapRoute(
"Unit",
"Home/Unit/{id}/{name}",
new { controller = "Home", action = "Unit", id = UrlParameter.Optional }
);
here I create the route in jquery, I add the route to a div with text 在这里我在jquery中创建路由,将路由添加到带有文本的div中
$('#nombreUnidad').append("<a href='Unit/" + param.Unidad.Id + "/" + param.Unidad.NombreUnidad + "'>Nombre: " + param.Unidad.NombreUnidad + "</a>");
so is my actionresult from my controller 我的控制器的动作结果也是
public ActionResult Unit(int id, string name)
{
return View("~/Views/Home/Unit.cshtml");
}
I want to get to that view by clicking on a div, yes, in this case the view will have a model, it is something like profiles, that is why in jquery I create the path over the text of a div to be able to return the view with the data of the unit that I chose 我想通过单击div进入该视图,是的,在这种情况下,该视图将具有模型,就像配置文件一样,这就是为什么在jquery中我在div文本上创建路径以能够用我选择的单位的数据返回视图
Apply this in the div click event. 在div click事件中应用它。
$.ajax({
type: "POST", //Post or Get
url: '@Url.Action("Action","Controller")',
data: JSON.stringify({ id:1, name: 'pp' }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
//code to handle the response
},
error: function () {
//code to handle error
}
});
Here is a cleaner way to approach this. 这是一种更清洁的方法。 You can use your jquery like this:
您可以像这样使用您的jquery:
$('#nombreUnidad').append(function(){
return $("<a>").attr("href", $(this).attr("data-appUrl") + ...).text("link text");
});
This code gets your URL directly from a data-appUrl attribute in your HTML. 此代码直接从HTML中的data-appUrl属性获取URL。 It is much easier to pass a URL directly into razor.
将URL直接传递到剃须刀要容易得多。 Something like this:
像这样:
<div id="nombreUnidad" data-appUrl="@Url.Content("~/Home/Unit/")">
<span>Here is some <b>sample text</b></span>
</div>
The code data-appUrl="@Url.Content("~/Home/Unit/")"
helps you make sure that the exact URL is being used for the current page. 代码
data-appUrl="@Url.Content("~/Home/Unit/")"
可帮助您确保当前页面使用的是正确的URL。
Here is a sample usage of the jquery: https://jsfiddle.net/xpvt214o/61226/ 这是jquery的示例用法: https : //jsfiddle.net/xpvt214o/61226/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.