in the following post: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html
<%= Html.Editor(prop.PropertyName) %>
Html.Editor creates a text box with MVC generated name. How do I get/access that name? id?
So in my code this gets generated:
<input type="text" value="Right" name="Template.RightColumn.ContainerName" id="Template_RightColumn_ContainerName">
and I want to generate this:
<div id="Template_RightColumn_ContainerName"></div>
so I need to access PropertyId. how do i do it?
thanks
I found it. you can use these:
<%: ViewData.TemplateInfo.HtmlFieldPrefix %>
or
<%: ViewData.TemplateInfo.GetFullHtmlFieldId("HtmlContent") %>
where HtmlContent is the name of your field. prefix will be generated automatically.
thanks
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 generate html textbox in this way:
<%: Html.TextBoxFor(m => m.Category) %>
ASP.NET MVC render html:
<input type="text" value="" name="Category" id="Category">
Is there is a way to set manually name of the textbox not eqaul to property "Category", but something else?
Thanks.
<%: Html.TextBox("someOtherName") %>
You have to change the default editor template or add a new template for this. You can refer this blog post from Brad Wilson for some insight.
Currently, I have code like this :
<% if (consumer.IsDischarged)
{ %>
<%= Html.ActionLink("<img src=\"../../Images/ConsumerImages/computer_go.png\" alt=\"discharged\" style=\"border:\"0\"/>", "Details", new { id = consumer.ApsId })%>
<%}
%>
Basically I want to show the hyperlinked image whenever the status of the isDischarged property of the consumer object is true. Any help or suggestions are highly appreciated.
a simple way could be
<a href="/Details/<%=consumer.ApsId%>"
<img src="../../Images/ConsumerImages/computer_go.png" alt="discharged" style="border:0"/>
</a>
You can try something like this. Use Url.Action to get the url and then you can put insert the hyperlinked image normally rather than trying to use Html.ActionLink.
<a href="<%= Url.Action("Details", new { id = consumer.ApsId }) %>">
<img src="../../Images/ConsumerImages/computer_go.png" alt="discharged" style="border:"0" />
</a>
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'm playing with an ASP.NET MVC application and I've run into a bit of a problem. I am pretty new to ASP.NET MVC and just barely understand the basics to get things to work at this point.
I have a PersonModel, a PersonController, and a bunch of views that let a user add a new person, edit a person and search for people.
I am not using a DataBase in the back end. Everything I'm doing depends on an external DLL that returns "person" structures (that I turn into PersonModels).
In order to search for people, I have to provide a person-structure that acts as search criteria to a method in the external DLL. The method returns a collection of person-structures that match the search criteria. If I want to retrieve all of the people in the system I supply an empty person-structure to the method.
So, I have the "retrieve all people" function working.....but I'd like to provide an advanced search.
My Search View is bound to a class that contains 2 properties:
Public Class PersonSearchModel
Private _searchCriteria As PersonModel
Private _searchResults As List(Of PersonModel)
Public Property SearchCriteria As PersonModel
Get
return _searchCriteria
End Get
Set(ByVal value As PersonModel)
_searchCriteria = value
End Set
End Property
Public Property SearchResults As List(Of PersonModel)
Get
return _searchResults
End Get
Set(ByVal value As List(Of PersonModel))
_searchResults = value
End Set
End Property
End Class
Now the Search View binds to this PersonSearchModel and I have 2 sections...a section where the user can provide search criteria and a section that displays the search results.
I am having a problem binding the PersonSearchModel.SearchCriteria to the controls used to display/gather the Person search criteria.
I cannot retrieve the search criteria.
This what I have in my view for the search criteria:
<fieldset>
<legend>Search Criteria</legend>
<%
With Model.SearchCriteria
%>
<div style="float:left">
<p>
<label for="FirstName">
FirstName:</label>
<%=Html.TextBox("FirstName", Html.Encode(.FirstName))%>
<%= Html.ValidationMessage("FirstName", "*") %>
</p>
<p>
<label for="LastName">
LastName:</label>
<%=Html.TextBox("LastName", Html.Encode(.LastName))%>
<%= Html.ValidationMessage("LastName", "*") %>
</p>
<!-- More controls -->
</div>
<% End With%>
</fieldset>
<%=Html.ActionLink("Search", "Search",Model.SearchCriteria)%>
<!-- The Search Results Section-->
The PersonModel passed into the Search method is a new/empty PersonModel Object.
It does not contain the data that the user entered.
What am I doing wrong here?
********** Edit **********
I have tried changing the View to bind differently. I removed the VB "With":
<fieldset>
<legend>Search Criteria</legend>
<div style="float:left">
<p>
<label for="FirstName">
FirstName:</label>
<%=Html.TextBox("FirstName", Html.Encode(.FirstName))%>
<%= Html.ValidationMessage("FirstName", "*") %>
</p>
<p>
<label for="LastName">
LastName:</label>
<%=Html.TextBox("LastName", Html.Encode(.LastName))%>
<%= Html.ValidationMessage("LastName", "*") %>
</p>
<!-- More controls -->
</div>
</fieldset>
<%=Html.ActionLink("Search", "Search",Model.SearchCriteria)%>
<!-- The Search Results Section-->
But this didn't help.
I also tried:
<fieldset>
<legend>Search Criteria</legend>
<div style="float:left">
<p>
<label for="FirstName">
FirstName:</label>
<%=Html.TextBox("Model.SearchCriteria.FirstName", Html.Encode(Model.SearchCriteria.FirstName))%>
<%= Html.ValidationMessage("FirstName", "*") %>
</p>
<p>
<label for="LastName">
LastName:</label>
<%=Html.TextBox("Model.SearchCriteria.LastName", Html.Encode(Model.SearchCriteria.LastName))%>
<%= Html.ValidationMessage("LastName", "*") %>
</p>
<!-- More controls -->
</div>
</fieldset>
<%=Html.ActionLink("Search", "Search",Model.SearchCriteria)%>
<!-- The Search Results Section-->
And:
<fieldset>
<legend>Search Criteria</legend>
<div style="float:left">
<p>
<label for="FirstName">
FirstName:</label>
<%=Html.TextBox("SearchCriteria.FirstName")%>
<%= Html.ValidationMessage("FirstName", "*") %>
</p>
<p>
<label for="LastName">
LastName:</label>
<%=Html.TextBox(".SearchCriteria.LastName")%>
<%= Html.ValidationMessage("LastName", "*") %>
</p>
<!-- More controls -->
</div>
</fieldset>
<%=Html.ActionLink("Search", "Search",Model.SearchCriteria)%>
<!-- The Search Results Section-->
However, I am still getting an empty/new PersonModel passed into the Search method in the controller. I've also checked the PersonSearchModel.SearchCriteria to see if maybe that contained the values entered, but this also has a new/empty PersonModel.
-Frinny
Using reflection is pretty much what the MVC model binders are setup to do, my guess is that you weren't naming your fields correctly so when they posted back to your action they didn't map up to your parameters. Try doing something like:
Function Search(ByVal personSearchModel As PersonSearchModel, ByVal collection As FormCollection) As ActionResult
Then your fields (HTML) should be named like so:
<%= Html.TextBox("personSearchModel.SearchCriteria.FirstName", Html.Encode(Model.SearchCriteria.FirstName)) %>
I think that you're missing the necessary prefixes on the calls to Html.TextBox and Html.ValidationMessage. I recommend not using VB's "With" keyword since it obscures the full name of the member. Both the HTML helpers and model binding (which is what gets used to pass parameters into action methods) need the full name of the property or field in order to retrieve the value.
Try this instead:
<%= Html.TextBox("SearchCriteria.FirstName", SearchCriteria.FirstName) %>
<%= Html.ValidationMessage("SearchCriteria.FirstName", "*") %>
Also, there's no need to call Html.Encode() for the value being passed into the TextBox - it gets automatically encoded anyway.
After much testing and debugging I discovered something interesting: I can retrieve the information entered by the user from the FormCollection passed into the Search Function. Originally my search function took 2 parameters. The first parameter was the PersonModel that was supposed bound to the PersonSearchModel.SearchCriteria, the second parameter was the FormCollection for the view.
I am able to create the PersonModel used for the PersonSearchModel.SearchCriteria based on the FormCollection passed into the Search function. I removed the first parameter (the PersonModel) since it was always a new/empty object.
This is my current Search method:
<AcceptVerbs(HttpVerbs.Post)> _
Function Search(ByVal collection As FormCollection) As ActionResult
Dim searchModel As New SearchPersonsModel
Dim personProperties() As PropertyInfo = GetType(PersonModel).GetProperties
For Each pi As PropertyInfo In personProperties
Dim piName As String = pi.Name
Dim info As String = Array.Find(collection.AllKeys, Function(x) x.Compare(piName, x, true) = 0)
If String.IsNullOrEmpty(info) = False Then
pi.SetValue(searchModel.SearchCriteria, collection.Item(info), Nothing)
End If
Next
'The following code uses the searchModel.searchCriteria to search for People.
End Function
My View (if your curious) looks like:
<% Using Html.BeginForm()%>
<%With Model.SearchCriteria%>
<fieldset>
<legend>Search Criteria</legend>
<div style="float: left">
<p>
<label for="FirstName">FirstName:</label>
<%=Html.TextBox("FirstName", Html.Encode(Model.SearchCriteria.FirstName))%>
<%=Html.ValidationMessage("Model.SearchCriteria.FirstName", "*")%>
</p>
<p>
<label for="LastName">LastName:</label>
<%=Html.TextBox("LastName", Html.Encode(Model.SearchCriteria.LastName))%>
<%=Html.ValidationMessage("Model.SearchCriteria.LastName", "*")%>
</p>
<!---..... more controls .... -->
</div>
</fieldset>
<%End With%>
<input type="submit" value="Search" />
<!-- Search Results Controls -->
<%End Using%>
This solution works but I am really not happy with it.
Why do I have to recreate the PersonModel used as the search criteria?
Why could I not pass this object as a parameter into the Search method?
-Frinny
Seems like UpdateModel() could be your friend here. MVC does not pass objects around web forms style.
Even if your Model consists of two objects, it's perfectly possible to use UpdateModel to retrieve the values for one of them. You just have to specify that object as parameter. E.g.:
Thing t = new Thing();
UpdateModel(t);
You may have to look at parameter names to allow MVC to guess properly.
Also, you may have to whitelist properties for security reasons and/or to escape overly keen model validation.