我有一個MVC操作querystrings奇怪的問題。以下是我有:爲什麼MVC查詢字符串丟失?
首先我有多個參數的動作:
public ActionResult Index(string p1, string p2, string action, string controller, string p5)
{
if (!String.IsNullOrEmpty(p1)
&& !String.IsNullOrEmpty(p2)
&& !String.IsNullOrEmpty(action)
&& !String.IsNullOrEmpty(controller)
&& !String.IsNullOrEmpty(p5))
{
return View();
}
else
{
TempData["error"] = "Values missing";
return View();
}
}
頁面的URL是這樣的: Home/Index?p1=1&p2=2&action=3&controller=4&p5=5
然後在視圖中我將送一個POST用一個簡單的beginform
using(Html.BeginForm("GetValues", "Home", FormMethod.Post))
{
@Html.HiddenFor(m => m.p1, new{ Value = Request.QueryString["p1"] })
@Html.HiddenFor(m => m.p2, new{ Value = Request.QueryString["p2"] })
@Html.HiddenFor(m => m.action, new{ Value = Request.QueryString["action"] })
@Html.HiddenFor(m => m.controller, new{ Value = Request.QueryString["controller"] })
@Html.HiddenFor(m => m.p5, new{ Value = Request.QueryString["p5"] })
@Html.HiddenFor(m => m.OtherField)
<button type="submit">Send</button>
}
<script>
$(function(){
$.ajax({
url: "/Home/OtherAction",
type: "GET",
cache: false,
success: function(result){
$("#OtherField").val(result.OtherField);
}
});
});
</script>
後,我博文的值,我收到他們在我這樣一個其他動作:
public ActionResult GetValues(ViewModel model)
{
if(ModelState.IsValid)
{
//I execute my code
}
else
{
//I have the p1,p2,action,controller,p5 correctly and OtherField is null, so I return to the Index action with the parameters I HAVE CORRECTLY
return RedirectToAction("Index", "Home", new{ p1 = model.p1, p2 = model.p2, action = model.action, controller = model.controller, p5 = model.p5})
}
}
因此,從技術上講,我用相同的參數重新定向到Index
,事件視圖正確返回,但頁面加載完成後。 我錯過了參數action and controller
即使我正在驗證如果爲空或空,我沒有問題(因爲操作參數中的值很好)。問題是,在瀏覽器中,我錯過了querystrings。
最終結果: Home/Index?p1=1&p2=2,p5=5
感謝
UPDATE:感謝@Stephen Muecke編輯我的問題,以便其他知道這是確切的問題。保留字爲action
和controller
,即使是字符串參數,包含參數和控制器的url也是用於路由網站的保留字,因此會自動將其省略。
請發佈視圖 – br4d
另外,張貼瀏覽器在重定向後顯示的網址。 – TSmith
已更新問題 –