MVC - Get ViewContext From Helper Method?
Feb 19, 2010
I would like to create a static helper method that I can call from a view.
Is it possible for a helper method to have access to the current ViewContext without needing to explicitly pass the ViewContext to the method as a parameter?
Something like HttpContext.Current except for ViewContext.
View 1 Replies
Similar Messages:
Mar 22, 2011
i have this code in a ViewUserControl, everything in between the foreach i want to pull out and call from a helper method...
[Code]....
View 4 Replies
Feb 24, 2011
I am using ASP.NET MVC 3 and the YUI library.I created my own helper method to redirect to an edit view by passing in the item's ID from the Model as such:
window.location = '@Url.RouteUrl(Url.NewsEdit(@Model.NewsId))';
Now I am busy populating my YUI data table and would like to call my helper method like above, not sure if it is possible because I get the item's ID by JavaScript like:
var formatActionLinks = function (oCell, oRecord, oColumn, oData) {
var newsId = oRecord.getData('NewsId');
oCell.innerHTML = '<a href="/News/Edit/' + newsId + '">Edit</a>';
};
View 1 Replies
Dec 30, 2010
I'm trying to write a controller method that returns an ActionResult.
In this method, i would like to:
1. call an HTML helper method
2. Capture and store the HTML helper's rendered HTML in a string
3. Return the method with the rendered HTML wrapped as a JSON
How do i call the Html Helper method from my controller method? Simply using the static class HtmlHelper does not work.
View 1 Replies
Mar 20, 2011
Can I call Ajax.BeginFrom from a custom helper method ?
AjaxHelper is not available in a custom helper method, so I tried to pass the "Ajax" available in ViewPage to Helper method while calling it, but then in method, BeginForm is not available on that passed "Ajax" parameter.
View 1 Replies
Mar 10, 2011
I am using ASP.NET MVC 3 and NUnit. I am trying to write a unit to test one of my helper methods. Here it is:
public static class UrlHelperAssetExtensions
{
private static readonly string yuiBuildPath = "http://yui.yahooapis.com/2.8.2r1/build/";
public static string YuiResetFontsGridsStylesheet(this UrlHelper helper)
{
return helper.Content(yuiBuildPath + "reset-fonts-grids/reset-fonts-grids.css");
}
}
Here is my unit test:
[Test]
public void YuiResetFontsGridsStylesheet_should_return_stylesheet()
{
// Arrange
RequestContext requestContext = new RequestContext();
UrlHelper urlHelper = new UrlHelper(requestContext);
// Act
string actual = urlHelper.YuiResetFontsGridsStylesheet();
// Assert
string expected = yuiBuildPath + "reset-fonts-grids/reset-fonts-grids.css";
Assert.AreEqual(expected, actual);
}....................................
View 2 Replies
Dec 2, 2010
Now I found out how to create custom html helpers
using System;
namespace MvcApplication.Helpers {
public class InputlHelper {
public static string Input(this HtmlHelper helper, string name, string text) {
return String.Format("<input name='{0}'>{1}</input>", name, text);
}
}
}
Now how to turn it into a strongly typed helper method InputFor Like it is in the framework?I don't need the Html.TextBoxFor method, I know it exists. I am just curious in how to implement this behavior myself and used this as a simple example.PS. I was looking in the mvc source code but couldn't find a trace of this mysterious TextBoxFor. I only found TextBox. Am I looking at the wrong code?
View 1 Replies
Aug 30, 2010
I am building a simple CMS with asp.net MVC and I have almost all the parts working. I have one minor issue which I can't work out how to solve. I have a Html helper method that renders the content of area. This method however uses the Response.Write to write its content rather than returning a string. The reason for this is that I'm performing a partial request and hence do not have a string to return.
My template body looks like this:
<body>
<h1>Default Template</h1>
<% Html.ContentArea("Main"); %>
</body>
The problem I'm having is that the content renders above the H1 tag. My understand was that the h1 should already be in the response and my call to response.Write() would therefore append content after that point. This is obviously not happening. What am I missing here.
I've used partial requests before and they always render in the correct place. I haven't yet used them with MVC 2, this could possibly be the problem.
Below is the content area extension method. The widget render method varies and is where the partial request occurs. However even the start and end container tag render above h1 so it has to be something fundamental I'm doing wrong.
public static void ContentArea(this HtmlHelper htmlHelper, string areaName)
{
var container = new TagBuilder("div");
container.GenerateId(areaName);
container.AddCssClass("content-area");
[Code]....
View 1 Replies
Apr 13, 2010
I am creating an Asp.net web site which will support dynamic data. When I am creating a dynamic web site from Scratch (from template in VS) all is working fine. But when I am trying to add dynamic entity (.edmx) file and running the application I am getting following error
"The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'. "
View 2 Replies
Jan 9, 2011
I followed MVC 2 source to create a CheckBoxList helper.
I was able to identify the problem I am having but not able to solve it.
On my view I have the following:
[Code]....
The problem is in the bold part ...
The newSelectList is equal to selectList but all items were deselected.
What am I missing?
View 3 Replies
Jan 20, 2010
I was wondering if it's possible to render an Html Helper in a View inside a codeblock. So instead of:
[code]....
And have this render. Of course as it is, it wont render, so is there a way to programically decide if a textbox can be added without having to have a million delimiters in the page to accomplish this?
View 2 Replies
Jul 1, 2010
I wanted to write a code for JQgrid in asp.net .I have tried the same in asp.net MVC,but i want it now in asp.net .n MVC we use Helper class,and return json data,so what to do in asp.net where helper class can not be used.Serialization can be done with data to get it in json.,but what about Htmlhelper.
View 2 Replies
Nov 26, 2010
I'm trying to use the Video helper (Microsoft.Web.Helpers) but nothing happens to Flash or Silverlight.
View 7 Replies
Feb 2, 2011
Can anyone help me with an alphabetical pager helper?
View 21 Replies
Mar 15, 2010
I am trying to create 2 Html helpers but that use the String Type as in MVC 2.
1. Html.FileFor Would render something like: <input type="file"
2. Html.Buttom and Html.Submit that would render <input type="button" or <input type="submit" In this case I suppose it makes no sense to have the For.
I know how to do (2) unless there would be something more than rendering the help and defining the Html attributes. However (1) I don't know how to do it
View 13 Replies
Mar 30, 2011
When is it appropriate to create a helper class for a sql server database using the asp.net framework? Basically I'm trying to create a minature wiki (with multiple pages) and storing all of the data/strings for a specific page into a table on it's own.
View 3 Replies
Jan 24, 2011
I need to use the standard ASP.NET membership database tables in plain winform projects, and I DO NOT WANT to add the ASP.NET dll, because my app must run with .NET Client framework, and not full framework. Does somebody knows of a helper class that links to ASP.NET tables in plain vanilla VB ot c# code? I mean with functions equivalent to CreateNewUser, CheckUserCredentials, isUserinRole, RoleList etc...
View 1 Replies
Jan 25, 2011
Is there a way to retrieve the client side ID for a model property similar to how the helpers like LabelFor etc generate the ID? For example, I have a model with a DateOfBirth property. I want to hook up that field to a jQuery DatePicker. Normally, I would hook up the date picker like this:
[Code]....
This works fine but hard codes the DateOfBirth property name. Ideally, I would like to use this:
[Code]....
Is this already available out of the box somewhere? I just created an extension method on the HtmlHelper class that does exactly this, but I wondered if there was already something built-in so I don't have to reinvent and test the wheel.
View 10 Replies
Oct 7, 2010
What is the best way to retrieve the display name attribute for an item in your model? I see a lot of people using the LabelFor helper for everything, but a label isn't appropriate if I just want to list the data out. Is there an easy way just get the Name Attribute if I just want to print it out in, say a paragraph?
View 1 Replies
Dec 5, 2010
I have created an image helper I call it , I see it but it is not clickable ,In the source view I c it as a href , but it is un clickable.
[Code]....
This is the helper in the view :
[Code]....
View 2 Replies
Mar 30, 2011
i am using html.textboxfor to display the amount (int) from my model. currently once the form is shown, the default amount is = 0. i wish to have the default set as 1, is it possible to overwrite the default for html helper??
[Code]....
View 1 Replies
Dec 20, 2010
This seems like a basic question, but my searches haven't turned up an answer. Is there a way to use a DataTable as the source for the WebGrid helper? The constructor wants an IEnumerable<dynamic>, which a DataTable is most definitely not.
View 2 Replies
Feb 8, 2011
I've created a new Route class, named SubdomainToAreaRoute, inherited from System.Web.Routing.Route, to handle subdomain.
The next implementation needed is to create a helper method to generate subdomain link.
For example:
Html.ActionLink("subdomain1", "account", "index") would generate:
http://subdomain1.example.com/account/index A series of other overloaded methods will also be implemented.
I undestand the geist of creating extension methods, which is one of the requirement to implementing these helper methods. Thus the main concern here is knowing what objects/classes to access and manipulate, and recommended algorithm to use.
There are some things that is unclear to me. For example, we register routes in the RouteTable.Routes (or simply route table), and whenever a url is requested, the url request is checked in the route table. If no url pattern matches, or if there is a matching pattern and the resource (e.g. web page) is missing, we receive a http error.
What is unclear here is in cases where specific physical path to a resource, like an image or javascript file is used for request. If these physical path url do not match the url patterns in the route table, how does an MVC application know to check for web application directory for the resource as the next step? As oppose to throwing an http error saying the resource can not be found.
This question may or may not affect my main concern here. But it is good to know if someone can explain it.
Focusing on registered routes, consider the following url route registration:
routes.Add("Home", new SubdomainToArea("", new { area = "Root", controller = "Home", action = "Index" } ) );
// route pattern 1
// intended to map to url: http://{area}.localhost.com
routes.Add("Default", new SubdomainToArea("{controller}/{action}", new { area = "Root", controller = "Home", action = "Index" } ) );
// route pattern 2
// intended to map to url: http://{area}.localhost.com/{controller}/{action}
routes.Add("Images", new SubdomainToArea("images/{*pathInfo}", new { area = "Root", controller = "Home", action = "Index" } ) );
// route pattern 3
// intended to map to url: http://{area}.localhost.com/images/{random path string}
How do I create helper methods to generate the appropriate url that matches these patterns?
I understand that extension methods for the UrlHelper class should be implemented to generate these url, and these url will be use in HtmlHelper class to generate the appropriate link tags.
In these helper methods, e.g. in the UrlHelper class, if passed a set of route values (such as controllers, action, areas and other url path details), how do we choose the appropriate route pattern that is registered in the route table, and then apply the route values to the pattern to generate the url?
How should I handle in cases where I don't specify one? What does the standard Html- Url- helper methods handle in case where no route name is not specified?
I understand the placeholder {controller}, {action} and {area} have special meaning and can be more easily identified and mapped to url pattern and so forth. How do we deal with custom placeholders we dynamically create, and use them for generating url?
View 6 Replies
Mar 17, 2011
What if any is the difference between @helper and @functions.They both appear to be used the exact same way, If your file is Named Extensions you use both like so.
[Code]....
View 1 Replies
Jun 1, 2010
I am learning how to work with BLL and DAL
I have 2 class files in BLL and using DAL i am connecting to the sql database
in BLL i have a class called productsHelper.cs and using GetProducts() method used to fetch records from database
products.cs - have all insert,edit,delete methods
I want to bind all products to gridview
But I couldnt find how to do that.how to get those methods to my aspx.cs page from class files.and write code in there.
View 5 Replies