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 !

John Smith
  • 1,194
  • 1
  • 12
  • 30

2 Answers2

0

Actionlinks can't post a form/data to a controller. All they do is create <a> tags. If you want to submit the form with an actionlink, you could use the @Ajax.ActionLinkhelper, or just post the form with jquery alltogether.

Also, this question has been asked lots of times before on stackoverflow, like here or here.

Community
  • 1
  • 1
Thousand
  • 6,562
  • 3
  • 38
  • 46
  • i tried this: `@Ajax.ActionLink(@j.ToString(), "resultWeb", "Result", new { val = j }, new AjaxOptions { HttpMethod = "Post" })` but i got the same error... – John Smith Jul 07 '13 at 10:01
  • i did this which is working: `using (Html.BeginForm("resultWeb", "Result", new { val = j }, FormMethod.Post)) { }` But its a button so its kinda ugly...is it possible to have only a text as input for submit ? – John Smith Jul 07 '13 at 10:11
  • im not quite sure i understand "having only a text as input for submit" ? – Thousand Jul 07 '13 at 14:53
-1

Thousands Answer is correct you cannot Post data via ActionLinks. If your FormsCollection is not too large then you can use Query Strings.

This is what I have done

Controller:

 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}

And finally the View

@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.
}
Flood Gravemind
  • 3,773
  • 12
  • 47
  • 79