TinyMCE with Ajax Form in ASP.NET MVC - asp.net-mvc

I have set up a form using TinyMCE in my MVC website. To do this, I have an ajaxForm in a partial view like this:
<% using (Ajax.BeginForm(
(Model.ViewMode == ViewMode.Insert) ? "Create" : "Edit",
new AjaxOptions()
{
UpdateTargetId = CustomerViewModel.WindowContentContainerId,
OnFailure = "addValidation"
//OnSuccess = "refresh"
}))
{%>
bla bla
<p>
<label for="CustomerBaneer">
Baner:</label>
<%= Html.TextArea(CustomerViewModel.FieldPrefix + "CustomerBaneer", Model.CustomerToEdit.CustomerBaneer)%>
<%= Html.ValidationMessage(CustomerViewModel.FieldPrefix + "CustomerBaneer", "*")%>
</p>
<input type="submit" value="Save" class="save" />
<%}%>
<script type="text/javascript">
tinyMCE.init({
mode : "textareas"
});
}
</script>
The tinyMce component render well, and I can change my text in bold, underline, etc.. However, when I click on save, the request is send with the textarea content without its formatting (I have monitored it with firebug). Why? Is there any HTML stripping function enables by default with ajax form?
Thanks.

U need to save text from tinymce to that textArea before submit.
Function OnBegin is too late, so i did it this way:
function tinyToText() {
ed = tinyMCE.getInstanceById('yourId');
if (ed) {
$("#yourId").val(ed.getContent());
}
}
<input type="submit" value="Send" onclick="tinyToText();" />

It looks like you need to add an OnBegin handler to your AjaxOptions to call tinyMCE.triggerSave() before your form submits. I'm more familiar with jQuery, so you may have to fix up the syntax for the Ajax.BeginForm calls.
new AjaxOptions()
{
UpdateTargetId = CustomerViewModel.WindowContentContainerId,
OnFailure = "addValidation",
OnBegin = "preSubmit"
//OnSuccess = "refresh"
}
<script="text/javascript">
function preSubmit() {
tinyMCE.triggerSave();
}
</script>

Related

Multiple submits to different MVC controller actions from buttons or dropdownlist changes

If I have a single form - with two submits:
From a save button - calls a form POST "Save" controller action.
From a change of a dropdown list value - calls a form POST "NoSave" controller action that returns a modified view without saving.
What's the best way of achieving this?
At the moment, I have the following - but they both call the same POST controller action. I want to call a named action for the dropdownlist update.
<form form method="POST">
<!-- dropdown list -->
<div class="row">
#Html.LabelFor(x => x.FieldName, "Field Name:")
#Html.DropDownListFor(x => x.FieldName, Model.FieldName, new { #class = "browser-default", #onchange = #"form.submit();" })
#Html.ValidationMessageFor(x => x.FieldName)
</div>
</div>
<!-- save button-->
<div class="save-button">
<input type="submit" class="btn" value="Save" />
</div>
</form>
what about using ajax request for different type of requests every type of request call different action or even different controller
[HttpPost]
public ActionResult SomeFunction(string a)
{
return Json("some data here", JsonRequestBehavior.AllowGet);
}
[HttpPost]
public ActionResult AnotherSomeFunction(string a)
{
return Json("some data here", JsonRequestBehavior.AllowGet);
}
//by click button
$("some button name ").click(function(){
$.ajax({
url: 'home/FirstAjax',
success: function(responce){ alert(responce.data)},
error: function(responce){ alert(responce.data)}
});
});
//by click another button
$("some button name ").click(function(){
$.ajax({
url: 'home/SecoundFirstAjax',
success: function(responce){ alert(responce.data)},
error: function(responce){ alert(responce.data)}
});
});
For this you can use ajax.beginform in first parameter you have to give the name of action and then controller and then some option which are like method type and success and failure actions.
#using (Ajax.BeginForm("_LoadPartial", "Abss", new AjaxOptions { HttpMethod = "POST", OnSuccess = "OnSuccess", OnFailure = "OnFailure" }))
{
<div class="row">
#Html.LabelFor(x => x.FieldName, "Field Name:")
#Html.DropDownListFor(x => x.FieldName, Model.FieldName, new { #class = "browser-default", #onchange = #"form.submit();" })
#Html.ValidationMessageFor(x => x.FieldName)
</div>
</div>
<!-- save button-->
<div class="save-button">
<input type="submit" class="btn" value="Save" />
</div>
}
Also provide OnSuccess and Failure Javascript fucntion on the same page.
<script>
function OnSuccess(){
// some action
}
function OnFailure(){
// some action
}
</script>

Kendo ui MVC window modal making Ajax call with paramters to controller

I am new to Kendo and I am having a very hard time trying to do this. I followed this demo.
here and got it to work up the part that I wanted to have a text box and a send button in the window modal that the user would have to fill in and click send.
I am able to use Ajax to make the call to my controller but I can't get the information in the modal window to pass to the controller via FormCollection. Here's my window modal template:
<script type="text/x-kendo-template" id="template">
<div id="details-container">
<h5 data-idtest="1">#= Id #</h5>
<h2>#= TitleDisplay # - #= ArtistDisplay #</h2>
Input The Day:<input id="TheDayTextBox" name="TheDay" type="text" />
#using (Ajax.BeginForm("TheAction", "Search", new AjaxOptions { UpdateTargetId = "#=Id" }))
{
<button class="btn btn-inversea" title="Log outa" type="submit">Log Offsdfsaf</button>
}
</div>
Controller:
public ActionResult TheAction(string id, FormCollection form)
{
....
}
So how does Kendo pass data to controller inside a modal?
Try putting your input inside of the actual form:
<script type="text/x-kendo-template" id="template">
<div id="details-container">
<h5 data-idtest="1">#= Id #</h5>
<h2>#= TitleDisplay # - #= ArtistDisplay #</h2>
#using (Ajax.BeginForm("TheAction", "Search", new AjaxOptions { UpdateTargetId = "#=Id" }))
{
Input The Day:<input id="TheDayTextBox" name="TheDay" type="text" />
<button class="btn btn-inversea" title="Log outa" type="submit">Log Offsdfsaf</button>
}

Post id attribute value to controller ASP.Net MVC

Suppose we have three hyperlink tag in asp.net mvc. Each of tag has an unique id attribute. How can I post value of id attribute of selected hyperlink to the controller?
Hyperlinks are like this :
A
B
C
The dialog just opens a form to upload an image and send the image to controller.
<script type="text/javascript">
$(function () {
$("#dialog").dialog({
bgiframe: true,
height: 170,
modal: true,
autoOpen: false,
resizable: false
})
});
and calling controller method:
<div id="dialog" title="A" >
<% using (Html.BeginForm("myMethod", "Controller", new { #Id = Model.Id }, FormMethod.Post, new { enctype = "multipart/form-data" }))
{%>
<p><input type="file" id="fileUpload" name="fileUpload" style="width:23;"/> </p>
<p><input type="submit" value="B" /></p>
<% } %>
</div>
I'm not forced only to send Id. However I have to send a different parameter when different hyperlink is selected to determine which of them is selected.
do something like this
A
B
C
<script>
function click(a){
var $a = $(a);
//and do anything here
//access the id like
var id = $a.attr('id');
}
</script>

ASP.NET MVC jQueryUI datepicker not working when using AJAX.BeginForm

I have an ASP.NET MVC Partial View that contains a Html.TextBox that is configured to use the datepicker from JQueryUI. This is done by ensuring the style is set to .datepicker. This all worked fine. However I have changed my forms to Ajax.BeginForm and included a Ajax.ActionLink that displays it after clicking on the link. Since adding this the datepicker does not display. In fact no JavaScript that previously worked is now even being invoked after a returning a partialview from the controller. Even if i PUT THE JavaScript/JQuery in the partial view itself it still does not use it. I really am confused, can someone please help?
Examples shown below
<div id="claims">
<div id="divViewClaims">
<% Html.RenderPartial("ViewClaim", Model.Claims ?? null); %>
</div>
<br /><br />
<div id="claim">
<% Html.RenderPartial("AddEditClaim", new Claim()); %>
</div>
</div>
Action Link, when clickon calls Controller Action to return PartialView, The JavaScript called on the OnSuccess fires but nothing else, that previously was hooked up by the document.ready function. All my scripts are in seperate files and referenced in master page.
<%= Ajax.ActionLink(string.Format("Add A{0} Claim", Model.Count > 0 ? "nother" : string.Empty), "AddClaim", "Driver", new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "claim", OnSuccess="showAddClaim" }, new { #class = "ControlLink" })%>
Controller Action
public ActionResult AddClaim()
{
return PartialView("AddEditClaim", new Claim());
}
Partial View, which shows the textbox with the style set to datepicker
<% var ajaxOptions = new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "divViewClaims", InsertionMode = InsertionMode.Replace, OnSuccess="hideAddClaim" }; %>
<% using (Ajax.BeginForm("AddEditClaim", "Claim", ajaxOptions, new { #name = "ClaimControl", #id = "ClaimControl" }))
{ %>
<fieldset>
<legend><%= Model.Id==0?"Add":"Edit" %> Claim</legend>
<p>
<label for="Title">Claim Date</label>
<%= Html.TextBox("Date", Model.Date.ToString().TrimEnd('0', ':', ' ') ?? "", new { #class = "datepicker" })%>
</p>
I appreciate any help on this.
If the element is created after document.ready you should re-match the element to jQuery.
Check out jQuery Live
What works for me is to use the OnSuccess property from the AjaxOptions. So, your code could be like this:
using (Ajax.BeginForm("AddEditClaim", "Claim", ajaxOptions, new { #name = "ClaimControl", #id = "ClaimControl", OnSuccess="the_javascript_function_below" })
where the_javascript_function is the name of a function that does this:
$(".datepicker").datepicker();

Problem binding action parameters using FCKeditor, AJAX and ASP.NET MVC

I have a simple ASP.Net MVC View which contains an FCKeditor text box (created using FCKeditor's Javascript ReplaceTextArea() function). These are included within an Ajax.BeginForm helper:
<% using (Ajax.BeginForm("AddText", "Letters",
new AjaxOptions() { UpdateTargetId = "addTextResult" }))
{%>
<div>
<input type="submit" value="Save" />
</div>
<div>
<%=Html.TextArea("testBox", "Content", new { #name = "testBox" })%>
<script type=""text/javascript"">
window.onload = function()
{
var oFCKeditor = new FCKeditor('testBox') ;
var sBasePath = '<%= Url.Content("~/Content/FCKeditor/") %>';
oFCKeditor.BasePath = sBasePath;
oFCKeditor.ToolbarSet = "Basic";
oFCKeditor.Height = 400;
oFCKeditor.ReplaceTextarea() ;
}
</script>
<div id="addTextResult">
</div>
<%} %>
The controller action hanlding this is:
[ValidateInput(false)]
public ActionResult AddText(string testBox)
{
return Content(testBox);
}
Upon initial submission of the Ajax Form the testBox string in the AddText action is always "Content", whatever the contents of the FCKeditor have been changed to. If the Ajax form is submitted again a second time (without further changes) the testBox paramater correctly contains the actual contents of the FCKeditor.
If I use a Html.TextArea without replacing with FCKeditor it works correctly, and if I use a standard Post form submit inplace of AJAX all works as expected.
Am I doing something wrong?
If not is there a suitable/straight-forward workaround for this problem?
The problem is unrelated to MVC but caused by using FCKeditor in conjunction with AJAX. To fix in the code above I added the following to the submit button's onclick event:
<input type="submit" value="Save" onclick="FCKeditorAPI.GetInstance('TestBox').UpdateLinkedField();" />
For more information see here.

Resources