MVC & SelectList - asp.net-mvc

Is there a way to output select list to a string in the View ?
//Javascript
var comboHtml = <%= Html.Encode(Model.MySelectList.ToHtml()) %>
or would i have to enumerate the list my self to create the html,
need this for a dynamic form field.

var comboHtml = '<%= Html.DropDownList("name", Model.MySelectList) %>';

You could use ajax and Html.RenderPartial.
(assuming mvc.net btw)
Also, the issue with doing it in Javascript is that the <%= stuff here %> would render the string at the page load.

Related

send value from hidden_field_tag in an ajax call

I have a hidden_field_tag
<%= hidden_field_tag 'render_date', #bikes.first.created_at %>
That translates to this in the DOM
<input id="render_date" name="render_date" type="hidden" value="2013-07-01 18:58:09 UTC">
How do I include the value of the input field in an ajax call so that it's accessible by the controller? (and how is it accessed)
<%= link_to 'update bikes', bikes_path, [something goes here], :remote=> true %>
Thanks for any help
You can try:
<%= link_to 'update bikes', bikes_path,
:variableName=>#bikes.first.created_at, :remote=> true %>
and remove the hidden field. In your controller access the variable with params[:variableName] keeping in mind the first time you go to the site there will be no value for params[:variableName] so you can't access is unless you use some sort of if else statement to make sure the value exists.
In your javascript you can use jquery like so:
var someData = $('#render_date').val();
$.ajax({
url: '/Controller/myAction',
type: 'POST',
data: { data: someData }
});
Then in your controller just make sure your action name takes in the data parameter. I don't know ruby but here is a c# example:
public ActionResult myAction(string data) {
//function code
}

Need Help on a custom HTML Helper

I'm trying to create a custom HTML Helper to help simplify my masterpages menu, however it is not rendering on the HTML when I use it.. I'm thinking I will need to create a partial view, any ideas?
I did this..
public static string CreateAdminMenuLink(this HtmlHelper helper, string caption, string link)
{
var lnk = TagBuilder("a");
lnk.SetInnerText(caption);
lnk.MergeAttribute("href", target);
return lnk.ToString(TagRenderMode.SelfClosing);
}
Now in my View, i have
<% Html.CreateAdminMenuLink("Home", "~/Page/Home"); %>
Thanks: Dave Swersky
Fix was: I forgot the equals and removed the semi-colon
<%= Html.CreateAdminMenuLink("Home", "~/Page/Home") %>
but when I look at the source, its empty.. tried adding <% using (Html.BeginForm()) %> and it adds a form.. but the link still doesnt come up.. debugged and the string works when i look at the watch, but does not render..
Any ideas?
Modify your markup:
<%= Html.CreateAdminMenuLink("Home", "~/Page/Home") %>
The equals sign and no semicolon should do the trick.

asp.net mvc parameter from page to a partial view

I'm with a problem, I have a ajax link that pass a parameter, but, the page that it opens does not need that parameter. The page only load 2 partial views, one of those need that parameter passed to the page to load the data correctly, and the other just need to load a form, so, don't need that parameter. How can i acheive this?
In order to do what you want, you will need to add the id to the ViewData construct.
var sysfunctions= UnisegurancaService.FunctionsRepository.All();
ViewData["NeededID"] = id
return View(sysfunctions);
then in your view where you render the partial
<%= Html.RenderPartial("GridFunction", (int)ViewData["NeededID"]) %>
Cast as required of course.
Whatever gets pushed in as the second param becomes the .Model in the partial. I would suggest also strongly typing your partials.
Try this:
<% Html.RenderPartial("GridFunction", new ViewDataDictionary {{"Id", ViewData["Id"]}}); %>
UPDATED:
And add this in your controller action:
ViewData["Id"] = Id;
UPDATED:
And in your GridFunction partial View you can access Id as:
<%= ViewData["Id"] %>
//Controller
public ActionResult EditFunctions(int id)
{
var sysfunctions= UnisegurancaService.FunctionsRepository.All();
return View(sysfunctions);
}
// This is the controller (it does no need the parameter "ID")
//This is the view "EditFunctions"
<div id="formFunction">
<% Html.RenderPartial("FormFunction"); %>
</div>
<div id="gridFunction">
<% Html.RenderPartial("GridFunction"); %> // The grid needs the ID to work correctly but its in the parent page not in the partial call....and the call is an ajax call
</div>
If some dependency of the page needs the parameter, then the page needs to know enough to pass the data in, so the page should be able to provide the data. Or, more simply, just add the parameter to the Page's viewdata and be done with it.

Dynamic RadEditor Creation through HtmlHelper

I am using the Telerik RadEditor (Q1 2009 SP1) in our ASP.NET MVC (RTM) project. The editor works great when rendered as a hardcoded object on the page with a static id. But when extending with an HtmlHelper to do dynamic creation by passing in an Id it seems to render the html as all lowercase for the tag. Does the HtmlHelper object mess with this innately by chance? The attributes look upper and lowercase respectively but this seems strange. Here is my code....thanks in advance!
<% if (placeholder.Type.ToLower() == "richtext") { %>
<%= Html.RadEditor("placeholder_" + placeholder.Name) %>
<% } else { %>
<%= Html.TextBox("placeholder_" + placeholder.Name, null, new { #class = placeholder.Type }) %>
<% } %>
The helper looks like this....
public static string RadEditor(this HtmlHelper html, string Id)
{
var sb = new StringBuilder();
sb.Append("<telerik:RadEditor ID='" + Id + "' Runat='server' DialogHandlerUrl='~/Telerik.Web.UI.DialogHandler.axd'>");
sb.Append("<Content>");
sb.Append("</Content>");
sb.Append("</telerik:RadEditor>");
return sb.ToString();
}
For the time being you cannot render RadEditor without having a valid Page object with a ScriptManager. We (Telerik that is) plan to add support for "standalone" rendering in the near future. Should be announced in a blog post so stay tuned.
The problem is the tag is a server side control. When you place it hardcoded in your page, the server side tag gets translated to html. When you're using the htmlhelper, you're outputting the html and it doesn't get processed as a server side tag.
If you want to do something dynamic, you should use a UserControl (.ascx file) and then use the Html.RenderPartial method.

How to prevent auto filling posted values in asp.net mvc ajax form

Inside of an asp.net mvc partial view, I have an Ajax form that posts a value and replaces the contents of its parent container with another instance of the form.
Index.aspx view:
<div id="tags">
<% Html.RenderPartial("Tags", Model); %>
</div>
Tags.ascx partial view:
<% using(Ajax.BeginForm("tag", new AjaxOptions { UpdateTargetId = "tags" }))
{ %>
Add tag: <%= Html.TextBox("tagName")%>
<input type="submit" value="Add" />
<% } %>
The controller:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Tag(string tagName) {
// do stuff
return PartialView("Tags", ...);
}
The problem is when the new instance of the form returns, the posted value is already stored in the input field. As in, whatever I posted as the 'tagName' will stay in the textbox. Firebug shows that the value is hardcoded in the response.
Is there any way to clear the input textbox's value when returning the partial view?
I've tried:
<%= Html.TextBox("tagName", string.Empty)%>
and
<%= Html.TextBox("tagName", string.Empty, new { value = "" })%>`
neither of which do anything.
EDIT:
I realize there are js solutions, which I may end up having to use, but I was wondering if there were any ways of doing it in the backend?
I'm not sure if this solution is "good enough" for you, but couldn't you just empty the form in a JS callback function from your ajax call? If you're using jQuery on your site, the callback function could look something like this:
function emptyFormOnReturn() {
$(':input').val();
}
I am not entirely sure if it will, but in case the above code also removes the text on your submit button, change the selector to ':input[type!=submit]'.
yes you should use jquery to set values on response
if you change your code to use jquery for ajax operations, you can call you settingvalues function on success callback...example:
http://docs.jquery.com/Ajax/jQuery.ajax#options

Resources