要查看的html actionLink参数

时间:2013-07-06 21:23:02

标签: c# html asp.net-mvc-3 razor

我是.Net中的菜鸟和所有网络开发:s 我使用html.BeginFormhtml.ActionLink时遇到问题。 我在homeWeb.cshtml中得到了这个:

@using (Html.BeginForm("resultWeb", "Result", new { val = 1 }, FormMethod.Post ))    
{ 
    <div class="main-block">
            <input style="width:100%;" type="text" name="searchValue" /><br />
            <div style="text-align: center;">
                <input type="submit" value="Submit" />
            </div>  
    </div>
}

它调用我的结果控制器和我的resultWeb视图发送val = 1作为参数 这是我的ResultController.cs:

[HttpPost]
        public ActionResult resultWeb(int val, FormCollection collection)
        {
            List<WebSite> list = new List<WebSite>();
            // Doing stuff with my list and the val
            return View(list);
        }

这部分工作正常,并将参数发送到我的视图。 问题是当我尝试在另一页上使用html.ActionLink做同样的事情时

resultWeb.cshtml:

<tr>
    @for (int i = 0; i <= Model.Count / 15; i++)
    {   
        int j = i + 1;
        <td>@Html.ActionLink(@j.ToString(), "resultWeb", new { val = j })</td>
    }
</tr>

当我点击其中一个链接时,它不起作用我收到了这个错误:

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.  
Requested URL: /Result/resultWeb/1

我想我做错了但我不明白。有人可以帮我吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

Actionlinks无法将表单/数据发布到控制器。他们只需创建<a>标签。 如果您想使用actionlink提交表单,可以使用@Ajax.ActionLink帮助程序,或者只使用jquery一起发布表单。

此外,此问题在stackoverflow之前已经被问过很多次,例如 here here

答案 1 :(得分:-1)

数千个答案是正确的,你不能通过ActionLinks发布数据。如果您的FormsCollection不是太大,那么您可以使用查询字符串。

这就是我所做的

<强>控制器:

 public ActionResult Index(string loc, string ma, string mo, int co = 0, int mi = 0)
        {
         search c = new search() { loc = loc, ma = ma, co = co, mo = mo, mi = mi }
         /*replace search() and query string variables with your FormsCollection*/
         /*Do thing here*/
         return View(DisplayModel)
        }

<强> MyModels

public class DisplayModel
    {
        public search Search { get; set; }
        public List<Website> Result { get; set; }
    }

public class Search
{... All my search variables in this model}

最后是视图

@model MyApp.Models.DisplayModel
<div>
    @using (Html.BeginForm("Index", "Buying", FormMethod.Get)){
    <fieldset>
        <legend>My form</legend>
    <input id="ma" name="ma" type="hidden" disabled="disabled" value="@Model.Search.ma" />
... The way you choose to display your your view. You can either keep the same form hidden or
<input type="submit" value="mybutton"/>>
</fieldset></div>
@foreach( var item in Model.Result)
{
... The way you choose to display your List.
}
相关问题