在MVC 4 ASP.NET C#中发布表单时的URL呈现? [英] URL Rendering at the time of form posting in MVC 4 ASP.NET C#?
本文介绍了在MVC 4 ASP.NET C#中发布表单时的URL呈现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我在发布表格后写了以下代码来呈现网址。网址应为http://test.com/Home/sum/10/12
,但显示为http://test.com/Home/sum?num1=10&num2=15 < br $>
控制器:
Hi All,
I have written following code to render url after posting the form. URL should be http://test.com/Home/sum/10/12
but is displaying as http://test.com/Home/sum?num1=10&num2=15
Controller:
public ActionResult Sum(int a, int b)
{
ViewBag.Message = "Calculation View :)";
ViewBag.a = a;
ViewBag.b = b;
ViewBag.sum = a + b;
return View();
}
Routingconfig:
routes.MapRoute(
"NewRoute", // Route name
"{Home}/{Sum}/{a}/{b}", // URL with parameters
new { controller = "Home", action = "Sum", a = UrlParameter.Optional, b=UrlParameter.Optional } // Parameter defaults
);
View:
@using (Html.BeginForm("Sum", "Home",new { num1 = 12, num2 = 13 },FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
<input type="submit" id="btnSum" name="btnSum" value="SUM"/>
}
或
or
<form id="test" name="test" action="/Home/Sum">
<input type="text" id="num1" name="num1" />
<input type="text" id="num2" name="num2" />
<input type="submit" id="btnSum" name="btnSum" value="SUM"/>
</form>
请帮帮我。紧急
谢谢,
Maddy
please help me out. Urgent
Thanks,
Maddy
推荐答案
RouteConfig.cs
file
file
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Sum", id = UrlParameter.Optional }
);
}
//Home Controller
public ActionResult Sum(int a, int b)
{
ViewBag.HomeDescription = "Welcome to MVC Application this is Home Controller";
ViewBag.Message = "Calculation View :)";
ViewBag.a = a;
ViewBag.b = b;
ViewBag.Sum = a + b;
return View(ViewBag.Sum);
}
//Sum.cshtml file
@{
ViewBag.Title = "Sum";
}
<h2>@ViewBag.HomeDescription;</h2>
<table>
<tr>
<th>
A
</th>
<th>
B
</th>
</tr>
<tr>
<td>
<input type="text" id="num1" name="num1" />
</td>
<td>
<input type="text" id="num2" name="num2" />
</td>
</tr>
<tr>
<td>
@ViewBag.Sum;
</td>
</tr>
</table>
最后网址应为
< a href =http:// localhost:61166 / Home / Sum?a = 10& b = 15> http://test.com/Home/Sum?a=10&b=15 [< a href =http:// localhost:61166 / Home / Sum?a = 10& b = 15target =_ blanktitle =New Window> ^ ]
这篇关于在MVC 4 ASP.NET C#中发布表单时的URL呈现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文