0

I'm a noob in .Net and all the web developpement :s I'm having an issue using html.BeginForm and html.ActionLink. I got this in my 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>
}

its calling my result controller and my resultWeb view sending the val = 1 as parameter here is my 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);
        }

this part is working and well sending the parameter to my view. The problem is when i try to do the same thing with an html.ActionLink on an other page

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>

And when i click on one of the links, it doesn't work i got this error:

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

I guess i'm doing something wrong but i don't understand what. Does someone can help me on this ?

Thanks !

4

2 回答 2

0

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

此外,这个问题之前在 stackoverflow 上已经被问过很多次了,比如这里这里

于 2013-07-06T23:09:23.013 回答
-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)
        }

我的模型

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.
}
于 2013-10-20T11:23:42.480 回答