ASP.NET MVC Html.SubmitImage - asp.net-mvc

I am looking for Html.SubmitImage in RC1 and don't see it anywhere when reflecting through the MVC assemblies.
Has it been moved / removed?

SubmitImage has moved to MvcFutures in RC1. You can find the actual source at http://www.codeplex.com/aspnet. The RC1 changeset is at http://aspnet.codeplex.com/SourceControl/changeset/view/21528

For others, this is a quick mockup of a SubmitButton extension for the HtmlHelper.
public static class HtmlLinkExtensions
{
public static string SubmitButton(this HtmlHelper htmlHelper, string imageLink, string value, string altText)
{
return string.Format("<input type='image' src='{0}' value='{1}' alt='{2}' />", imageLink, value, altText);
}
}

Related

Access WebViewPage from custom HTML Helper

I've got a ASP.NET MVC 3 Razor Web Application.
I've got a WebViewPage extension:
public static bool Blah(this WebViewPage webViewPage)
{
return blah && blah;
}
And i want to access this from my HtmlHelper extension:
public static MvcHtmlString BlahHelper(this HtmlHelper htmlHelper, string linkText, string actionName)
{
// how can i get access to the WebViewPage extension method here?
}
I can of course duplicate the functionality of the WebViewPage extension if i had to, but just wondering if it's possible to access it from the HTML helper.
// Warning: this cast will crash
// if you are not using the Razor view engine
var wvp = (WebViewPage)htmlHelper.ViewDataContainer;
var result = wvp.Blah();
You should change that method to extend HttpContextBase, which you can access from both HtmlHelper and WebViewPage.
I would try:
((WebViewPage)htmlHelper.ViewContext.View). Blah()
I had the same problem and the accepted answer led me to the solution (+1). Maybe this hint helps somebody else too.
Additionally I had to use the generic version of WebViewPage inside a strongly type view.
Otherwise I got a type cast exception.
public static MvcHtmlString ToBodyEnd<TModel>(this HtmlHelper<TModel> htmlHelper, ...) {
var vp = (DerivedWebViewPage<TModel>)htmlHelper.ViewDataContainer;
//... more code ...
}

Turn off HTML Encoding in Razor

I have a function that returns a snippet of JavaScript and/or HTML.
static public string SpeakEvil()
{
return "<script>alert('BLAH!!');</script>";
}
In the view, Razor is quite rightly HTML encoding it, as most would expect.
#StaticFunctions.SpeakEvil()
How do I have Razor not HTML Encode this, so that the HTML and JavaScript are emitted verbatim, and that any script actually runs?
You could use the Raw() function but it's mostly meant for things that come from the database.
For a helper like you have I would suggest returning an IHtmlString:
static public IHtmlString SpeakEvil() {
return new HtmlString("<script>alert('BLAH!!');</script>");
}
That way you don't have have to call Raw() at every callsite.
Use the Html.Raw helper.
#Html.Raw(StaticFunctions.SpeakEvil())
Return a MvcHtmlString (Inherits from HtmlString) by calling the MvcHtmlString.Create() method like so:
public static MvcHtmlString SpeakEvil()
{
return MvcHtmlString.Create("<script>alert('BLAH!!');</script>");
}
You could also make it into an String extension:
public static MvcHtmlString HtmlSafe(this string content)
{
return MvcHtmlString.Create(content);
}
Source:
http://geekswithblogs.net/shaunxu/archive/2010/04/10/lt-gt-htmlencode-ihtmlstring-and-mvchtmlstring.aspx

TempDataExtensions from the winter of 2007---still needed in MVC2?

This extremely cool article written in the winter of 2007 shows me this code:
public static class TempDataExtensions
{
public static void PopulateFrom(this TempDataDictionary tempData, object o)
{
foreach (PropertyValue property in o.GetProperties())
{
tempData[property.Name] = property.Value;
}
}
public static void PopulateFrom(this TempDataDictionary tempData
, NameValueCollection nameValueCollection)
{
foreach (string key in nameValueCollection.Keys)
tempData[key] = nameValueCollection[key];
}
public static void PopulateFrom(this TempDataDictionary tempData
, IDictionary<string, object> dictionary)
{
foreach (string key in dictionary.Keys)
tempData[key] = dictionary[key];
}
public static string SafeGet(this TempDataDictionary tempData, string key)
{
object value;
if (!tempData.TryGetValue(key, out value))
return string.Empty;
return value.ToString();
}
}
I'm not seeing any code like this in the MVCContrib source or in MVC2 source. This makes me think that I can still use this pattern now without fear of the equivalent functionality already living in the current MVC2 release (might be in MVC3 Preview 1?).
I did not see any update edits to the article. Does this MVC code from 2007 stand the test of time? Is it still ready for now?
Yes, this will work and this functionality is not replaced.
One caveat. In MVC 1 Temp data stayed around for one request only. With MVC 2 tempdata now stays around until you access it or manually clear it. This could complicate things if your redirect fails or never reads the tempdata.
The new dynamic keyword will also provide similar functionality maybe the new new C# 4.0 dynamic type may clean things up a little.

Using ASP.NET MVC Html Helpers in a standard web forms project

So for example in the code behind of a web form aspx page I would like to be able to do things like
string textBoxHtml = Html.TextBox("MyTextBox");
Is this possible?
Is the source code available to fork for webforms?
Possible? Yes.
The entire MVC source is available at:
http://www.microsoft.com/downloads/details.aspx?FamilyID=53289097-73ce-43bf-b6a6-35e00103cb4b&displaylang=en
Good luck!
You'll quickly find that pulling bits of code out of MVC is like only wanting a banana and getting the gorilla holding it. ;)
Here's something that is working for me so far.
public static class PageCommon
{
public static System.Web.Mvc.UrlHelper GetUrlHelper(this System.Web.UI.Control c)
{
var helper = new System.Web.Mvc.UrlHelper(c.Page.Request.RequestContext);
return helper;
}
class ViewDataBag : IViewDataContainer
{
ViewDataDictionary vdd = new ViewDataDictionary();
public ViewDataDictionary ViewData
{
get
{
return vdd;
}
set
{
vdd = value;
}
}
}
public static System.Web.Mvc.HtmlHelper GetHtmlHelper(this System.Web.UI.Control c)
{
IViewDataContainer x;
var v = new System.Web.Mvc.ViewContext();
var helper = new System.Web.Mvc.HtmlHelper(v, new ViewDataBag());
return helper;
}

How to use Data Annotation Validators in Winforms?

I like the Validation Application Block from the Enterprise Library :-)
Now i would like to use the DataAnnotations in Winforms, as we use asp.net Dynamic Data as well. So that we have common technologies over the whole company.
And also the Data Annotations should be easier to use.
How can I do something similiar in Winforms like Stephen Walter did within asp.net MVC?
I adapted a solution found at http://blog.codeville.net/category/validation/page/2/
public class DataValidator
{
public class ErrorInfo
{
public ErrorInfo(string property, string message)
{
this.Property = property;
this.Message = message;
}
public string Message;
public string Property;
}
public static IEnumerable<ErrorInfo> Validate(object instance)
{
return from prop in instance.GetType().GetProperties()
from attribute in prop.GetCustomAttributes(typeof(ValidationAttribute), true).OfType<ValidationAttribute>()
where !attribute.IsValid(prop.GetValue(instance, null))
select new ErrorInfo(prop.Name, attribute.FormatErrorMessage(string.Empty));
}
}
This would allow you to use the following code to validate any object using the following syntax:
var errors = DataValidator.Validate(obj);
if (errors.Any()) throw new ValidationException();

Resources