How to accept html input through textarea in ASP.NET MVC 2.0 - asp.net-mvc-2

I am using ASP.net MVC 2.0 , i have used a textarea on my View like
<%= Html.TextAreaFor(m => m.Description, 7, 35, new { #class = "bg_area normal" })%>
it shows me an exception while submitting the form after input html data like
<p><b> hello world ! </b></p>
is there any way to accept such kind of data using textarea ?
Is there any way to handle it from one place or, i have to add
[ValidateInput(false)]
to each action ?

If you want to post HTML to a Controller method you need to add the [ValidateInput(false)] attribute to your controller method.
Here is the MSDN documentation.

I am assuming you are getting
A potentially dangerous Request.Form value was detected from the
client... exception. IF this is the case then you need to decorate your action result with
[ValidateInput(false)]

Related

CKeditor used on form input

Is it possible to use ckeditor on a form input instead of textarea, i am building a CMS and now trying to add ckeditor and majority of of fields are form input not textarea
Thanks in advance
You can use CKEditor on an element (a div, say) that has contenteditable set. In fact, by default contenteditable elements will have CKEditor editors instantiated. It seems unconventional to use a rich text editor on an input of type text but I imagine it could be done.
As far as I know CkEditor has to be created using a textarea. In saying this, I am using it in a Razor MVC view and its one of the classes in my form..
The request will be blocked though and you will get this error;
A potentially dangerous Request.Form value was detected from the
client
To get around this, you need to get the value of the CKEditor text and html encode it. You can do this
when submitting your form, intercept it on the onsubmit function:
<form id="myform" onsubmit="javascript:onFormSubmit(this);return false" >
...
</form>
Then in this onFormSubmit function
Get the value,
Set the property value to the url encoded value
Do a ajax call to your server with the data
Your function will get the CKEditor encoded value like so:
function onFormSubmit(form)
{
var editor = CKEDITOR.instances["EditorId"];
var richtextValue = editor.getData();
var urlEncodedValue = encodeURIComponent(richtextValue);
// TODO rest of code doing ajax post to submit your form and its data
// Here you need to do an ajax call to your server pass it the form data along with the url encoded CKEditor value for that property
}

Custom Form in Custom Joomla Component

I have a basic front-end form within a component that I created using ComponentCreator.
How and where do I direct the form "action"? Where can I handle this so that I am following the MVC design pattern? My form is within a view (default.php)
The form should simply insert to the database upon submission. I don't want to use a form building extension, I have tried these and they don't meet my requirements.
What version of Joomla? Assuming your not using AJAX then just direct to your controller.
<form id='MyForm' method='post' action='index.php?option=com_mycomponent&task=myoperation'>
<input type='text' name='myname'/>
...</form>
Then you will have a function in your controller (controller.php) called myoperation that will process all the things in your form (not forgetting the token of course)
JSession::checkToken('request') or jexit( JText::_( 'JINVALID_TOKEN' ) );
$jinput = JFactory::getApplication()->input;
$add_name = $jinput->get('myname', '', 'STRING');
//whatever you need to do...
//add to model if you wanted to add name to DB
$model = &$this->getModel();
$model->updateDB(...);
Then create function updateDB in model.
This is a rough example with Joomla 2.5 but should work with 3.x. Hope this helps.

Asp MVC 2: Typed Editor Template

(I reference this tutorial in this text)
I want to use the Html.EditorFor (or Html.Editor) helpers.
If a UserControl needs additional data it is passed via
...EditorFor(model => model.Album, new { Artists = Model.Artists, ... })
In the UserControl it's accessed via ViewData[stringKey], ie
... new SelectList(ViewData["Artists"] as IEnumerable, ...
To me this smells a little fishy as I would prefer a strongly typed ViewModel which ensures that specific data is available.
I'm now a little bit stuck as I don't know wheater there's a "typed way" to find or I should accept this way as-is.
How did you solve this issue? Any help appreciated!
Lg
warappa
I would probably change my view model so that I don't need to pass this additional information. You could make for example an album has a collection of artists. Now all tha you will have to do is:
<%: Html.EditorFor(model => model.Album) %>
And in your editor template:
<%: Html.DropDownListFor(x => x.SelectedArtist, new SelectList(Model.Artists)) %>

Enabling/Disabling CSS based on Session value using MVC

I am rather new to MVC applications, and one thing I am trying to accomplish is enabling or disabling stylesheets based on a Session value.
I have stylesheets referenced in my Site.Master page in this manner:
<%=Html.Stylesheet("~/styles/main.css", "string")%>
<%=Html.Stylesheet("~/styles/additions.css", "string")%>
Right now, for testing, I have been putting an if statement around the Html.Stylesheet tags saying:
<% if (Session["cssRule"] = "enableCss") { %>
<%=Html.Stylesheet("~/styles/main.css", "screen")%>
<%=Html.Stylesheet("~/styles/additions.css", "screen")%>
<%} %>
So if the 'cssRule' Session value is null, no CSS loads. Currently this is working fine, but it is not exactly what I am looking for. Right now I set the Session value in the Controller when the user logs in, but ultimately I need to set the value of the Session variable depending on if a user clicks the enable or disable button. Since I have been primarily using webforms for the past year and a half, I just want to drop a hyperlink and set an event for it, but alas, this is the future of MVC so I need to figure out how I can do something like this..
So my real question is how can I set the Session of the "cssRule" value by clicking a link using MVC?
I'll assume you want to use a standard link (not ajax) and that your main view is Index
Just add a method in your controller (pseudocode)
public ActionResult ToggleCSS()
{
if (Session["cssRule"] != null && Session["cssRule"] == "enableCSS")
{
Session["cssRule"] = "disableCSS";
}
else
{
Session["cssRule"] = "enableCSS";
}
return View("Index");
}
Then in your view, use
<%= Html.ActionLink("ToggleCSS", "ControllerName") %>
You can use lots of fancy different methods to obtain the same result; use ajax and relaod the page, or not, or redirect to a page listing css files to apply, etc... but this one should work :)

MVC ActionResult to use when returning html content

I have a some html fragment in memory that I need to render using RenderAction. For instance my action method looks something like this:
var html = "some html code
How do I make it render this html by using RenderAction, I don't want to create a view file because this is dynamic html. And I can't use any of the file results because this is not coming from a file. What other ways are there to accomplish this?
Check out the ContentViewResult, or the shortcut return Content(string html)
Immediately I can think of two options:
1) You can read this HTML string into a stream and then serve it back via FileResult (stream, "text/html")
2) You read this HTML string into a model property and output it through a view via <%= Model.MyDynamicHtml %>
The 1) is probably easier.