in my route table I have this entry
routes.MapRoute(
"myRoute",
"route/{controller}/{action}/{id}/{start}/{end}",
new { controller = "Home", action = "Index", id = "", start="", end="" }
);
in my master page I have a line of code like so:
<%= Html.TextBox("foo", "bar") %>
If I access the page in the form of http://mysite.com/route/Home/Index/id/start/end the textbox renders OK with a value of "bar"
However if I access the page using the default parameters http://mysite.com/route/ the textbox does not have a value! In the emitted HTML it shows up like so:
<input id="foo" type="text" value="" name="foo"/>
it didn't set the value to "bar"...is this a bug? or is this not allowed in mvc master pages?
it should work fine
" <%= Html.TextBox("name", "Please enter your name...")%>
Output : < input id="name" name="name" type="text" value="Please enter your name..." />
<%: Html.TextBox("foo", "bar") %>
sometimes you need to force it to be a simple html attribute as follows
<%: Html.TextBox("foo", null,new{value="bar"}) %>
Related
how can i post the form values using html.actionlink, don't want to use routes dictionary
<%=Html.ActionLink("Download", "MyFiles", "Jobs", null, new { #class = "cvclick" })%>
A link points to an HTTP GET request.
An HTTP GET request is sent to a URL; the URL must be defined using a route.
To POST values you could use an HTML form:
<% using (Html.BeginForm("MyFiles", "Jobs")) { %>
<%= Html.Hidden("key1", "value1") %>
<%= Html.Hidden("key2", "value2") %>
<input type="submit" value="Download" />
<% } %>
To POST values you could use Spark View Engine and a HTML form:
<form action="myfiles" controller="jobs">
<hidden name="key1" value="value1" />
<hidden name="key2" value="value2" />
<submit title="Download" />
</form>
(The code uses some pretty standard bindings that be wired in Spark)
As for links and ActionLink. I would use the ajax helper instead since it can POST stuff. (Ajax.ActionLink)
Edit
So you want to DOWNLOAD a file? Well. The link should point on an action in your controller. The action should return a FileResult with your file. See here: http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.file(v=vs.90).aspx
You can post the data using :-
anthing inside Beginform will be posted
<% using (Html.BeginForm("ActionName", "ControllerName"))
{ %>
texbox code
<input type="submit" class="dASButton" value="Submit" />
<% } %>
I am trying to create a simple search box that results in something like http://www.example.com/Search?s=searchTerm I have the routing setup so that it accepts a url like this, and does the right thing. The problem I'm having is getting the form to create the querystring. I have tried many variations of:
<% using (Html.BeginForm("Search", "Home", FormMethod.Get, new { ???? })) {%>
<input id="submitSearch" class="searchBox" type="text" runat="server"/>
<input type="submit" value="Search" /> <%} %>
I'm not sure how to setup the Html.BeginForm so it grabs the submitSearch value and passes it to /Search?s=valueHere. This seems like I am missing something easy.
You need to set name on the input box to s.
<% using (Html.BeginForm("Search", "Home", FormMethod.Get, new { })) { %>
<input id="s" name="s" class="searchBox" type="text" />
<input type="submit" value="Search" />
<% } %>
Also, note that I changed the id to s as well, since common practice is to have the same value for name and id. However, it is only the name attribute that affects the query string name in the request.
And as David noted in a comment, the runat="server" is not needed in ASP.NET MVC.
I have the following code:
<% using (Html.BeginForm("AddComment", "Comments", FormMethod.Post)) { %>
<div id="New_Comment">
<textarea name="newComment" id="newComment">Add comments</textarea>
<input type="submit" value="Add" />
<div><span class="text_grey">Attach:</span>File Link</div>
</div>
<%} %>
This is in a partial rendered by the MyPage controller. For some reason the action on the form comes out blank, if I reference a method on the MyPage controller it works fine what I want to do is point to a different controller with my form.
To solve this issue I simple added in an area route value like so:
new { area = "" }
With the empty string directing the route to the default area.
1) Is your "Comments" action marked as being a POST action?
2) Also
Try just doing:
<% Html.BeginForm("AddComment", "Comments"); %>
// Html and script
<% Html.EndForm(); %>
I know that there shouldn't be difference between what you have and what I suggest, but it's worth a try.
I am playing with Sparkview 1.0 on ASP.NET 4.0 with MVC2. Trying to create a simple HTML form.
When the form loads, it renders as expected. Click Save button on the form, the model validates, returns an error about length of field (expected) but then the !{ Model.Name } tag gets rendered as the text ${ Model.Name } rather than the actual expected output of "test blah blah".
Where can I find an example of creating HTML forms using the HTML helpers with Sparkview?
!{ Model.Name }
${ Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") }
# Html.BeginForm();
<fieldset>
<legend>Fields</legend>
<p>
<label for="Name">Name:</label>
!{ Html.TextBox("Name", Model.Name) }
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
# Html.EndForm();
And here is the controller method:
[HttpPost]
public ActionResult Add(Project project)
{
if(ModelState.IsValid)
{
// save to db
Response.Redirect("/");
}
ViewData["Model"] = project;
return View();
}
Model is a special name in Spark, it represents a strongly typed model for your view.
Therefore, you can either change the name of your dictionary key to something other than Model (call it ViewData["SuperModel"]:) or simply return the strongly typed viewresult overload of the view method.
I have multiple pages containing the same partial view. The partial contains a form that posts to an action. After a post I want to return to the page I was on before the post. What's the best way to do this?
Example:
Partial View:
form post action = note/create/
Pages
page1:
products/index/
page2:
customer/details/
page3:
order/details/
These 3 pages contain the partial view, when posting the partial it redirects to note/create/. I need to return to the original page on success.
Thanks
Simon
Either have the post happen via AJAX -- thus not leaving the page, or pass the current controller/action/id (or the Url as a whole) as parameters to the action that handles the post. See below for an example of the later.
<% using (Html.BeginForm(...)) { %>
<input type='hidden'
name='currentController'
value='<%= ViewContext.RouteData["controller"] %>' />
<input type='hidden'
name='currentAction'
value='<%= ViewContext.RouteData["action"] %>' />
<input type='hidden'
name='<%= ViewContext.RouteData["id"] %>' />
...rest of form...
<% } %>
or
<% using (Html.BeginForm( ...,
new { ReturnUrl = Url.Action( ViewContext.RouteData["action"],
ViewContext.RouteData ) }, ... )) { %>
....
<% } %>
You can store the current page address in a hidden field and send it with Post request.
In your partial view:
<script type="text/javascript">
var field = document.getElementById("currentPage");
field.value=document.location.href;
</script>
<form method="post" action="note/create/">
...
<input type="hidden" value="" id="currentPage" name="currentPage" />
</form>
Then retrieve the address of the hidden input and redirect the user to it.