Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy doesn't this form call my controller action in ASP.NET MVC?
    text
    copied!<p>I'm attempting to make a TinyURL clone in ASP.NET MVC as a learning project.</p> <p>Right now, all I want is to be able to submit new URLs to my /Home/Create action via a form.</p> <p><img src="https://imgur.com/JJOP0.png" alt="alt text"></p> <p>I have my LINQ expression all setup, I have my routing setup, and I have my view setup but something is wrong with my setup. </p> <p>Routing:</p> <pre><code> routes.MapRoute( "Default", // Route name "", // URL with parameters new { controller = "Home", action = "Index" } // Parameter defaults ); routes.MapRoute( "Redirect", "{hash}", new { controller = "Home", action = "RequestLink", hash = "" } ); </code></pre> <p>These routes allow me to be able to go to my website, www.tinyurlclone.com/ and if nothing is passed ti will simply go to my Home/Index() action. However, if you put anything after the slash, it will consider that a Link Hash and attempt to retrieve the hash.</p> <p>My HomeController is as follows:</p> <pre><code>[HandleError] public class HomeController : Controller { TinyGetRepository repo = new TinyGetRepository(); public ActionResult Index() { return View(); } public ActionResult Create(String url) { String hash = repo.addLink(url); ViewData["LinkHash"] = hash; return View(); } public ActionResult RequestLink(String hash) { String url = repo.getLink(hash); return Redirect(url); } } </code></pre> <p>My repo class has all my LINQ expressions in it for dealing with the database and I don't really need to include them because it isn't relevant to this question.</p> <p>Finally, my basic Home/Index() view (used for submitting urls) is as follows:</p> <pre><code>&lt;%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" &gt; &lt;head runat="server"&gt; &lt;title&gt;Index&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div&gt; &lt;center&gt; &lt;span style="font-size: 14pt"&gt;TinyGet &lt;em&gt;(beta)&lt;/em&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="font-family: Tahoma"&gt;Reduce your long links to smaller ones to keep them more memorable....&lt;br /&gt; &lt;/span&gt; &lt;% using(Html.BeginForm("Create", "Home")) %&gt; &lt;% { %&gt; &lt;%= Html.TextBox("url") %&gt; &lt;input type="submit" name="submitButton" value="Shorten Link!" /&gt; &lt;% } %&gt; &lt;/center&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>However, my form simply isn't firing any methods when I click submit.</p> <p>Furthermore, if I view the source of my generated HTML I can see that it didn't make my Form's action correctly, it reads:</p> <pre><code>&lt;form action="" method="post"&gt;&lt;input id="url" name="url" type="text" value="" /&gt; &lt;input type="submit" name="submitButton" value="Shorten Link!" /&gt; &lt;/form&gt; </code></pre> <p>Why is the HTML helper putting "" as the action when it ~should~ be putting /Home/Create? Why isn't my /Home/Create action method being called? Even if I don't use the Html helpers and specify the <code>&lt;form</code>> tag manually it throws errors.</p> <p>What is wrong here?</p> <p>Source for project: <a href="http://www.filedropper.com/tinyget" rel="nofollow noreferrer">here</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload