I am working on asp.net MVC 2 application. I have ajax.action link but it is not working. I have this code in my view:
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
<script type="text/javascript">
function success(result) {
alert(result);
// TODO: do something with the object
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%: Ajax.ActionLink(
"Delete",
"Delete",
new { Id = 55 },
new AjaxOptions { OnComplete = "success" })
%>
</asp:Content>
and this is controller code:
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
public JsonResult Delete(Int32 Id) {
return Json("Record deleted!", JsonRequestBehavior.AllowGet);
}
But when I click the link, It shows Record deleted! in browser instead of showing as alert. Am I missing some file(s) ?
You might need to include the MicrosoftAjax.js and MicrosoftMvcAjax.js scripts to your page:
<script type="text/javascript" src="<%= Url.Content("~/scripts/MicrosoftAjax.js") %>"></script>
<script type="text/javascript" src="<%= Url.Content("~/scripts/MicrosoftMvcAjax.js") %>"></script>
In ASP.NET MVC 3 and later those files are obsolete and replaced by the jQuery unobtrusive scripts.
Now that you have sent me your sample project by mail it is clear what the problem is. You have defined your success javascript function inside the <title> tag of your page which obviously cannot work.
So inside your MasterPage you should define a special placeholder for your scripts (notice also how I have fixed your hardcoded links to the CSS file with a url helper):
<head runat="server">
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
<link href="<%= Url.Content("~/Content/Site.css") %>" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<%= Url.Content("~/scripts/MicrosoftAjax.js") %>"></script>
<script type="text/javascript" src="<%= Url.Content("~/scripts/MicrosoftMvcAjax.js") %>"></script>
<asp:ContentPlaceHolder ID="Scripts" runat="server" />
</head>
that you could override in your view:
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Scripts" runat="server">
<script type="text/javascript">
function success() {
alert('success');
// TODO: do something with the object
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%= Ajax.ActionLink(
"Delete",
"Delete",
new { id = 55 },
new AjaxOptions { OnComplete = "success" }
) %>
</asp:Content>
I am trying to create a menu using <ui><li></li></ui> tags. I am reading from an XML file and storing it in a List of(menu) object.
Menu Object:
Public Class MenuVM
#Region "Properties"
Public Property ID As Integer
Public Property Description As String
Public Property Comments As String
Public Property UserControlName As String
Public Property AssemblyName As String
Public Property ModelName As String
Public Property SortOrder As Integer
Public Property IsSelected As Boolean
#End Region
End Class
HomeController Class:
Public Class HomeController
Inherits System.Web.Mvc.Controller
Function Index() As ActionResult
Dim oMenuHelper As New MenuHelper
Return View("index", oMenuHelper.BuildMenu())
End Function
<HttpPost()> _
Function Index(ByVal iSelect As Integer) As ActionResult
Dim oMenuHelper As New MenuHelper
Return View("index", oMenuHelper.BuildMenu())
End Function
Function About() As ActionResult
Return View()
End Function
End Class
ASPX PAGE:
<%# Page Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of List(OF MVCApp.MenuVM))" %>
<%# Import Namespace ="MVCApp" %>
<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<div id="menu" class="block">
<% Using Html.BeginForm("Index", "Home")%>
<ul id="menuItems">
<% For Each item As MVCApp.MenuVM In Model%>
<li><%: Html.ActionLink(item.Description, "Index", "Home", New With {item.ID}, Nothing)%></li>
<%Next%>
</ul>
<% End Using %>
</div>
</asp:Content>
The output:
When You click on view source the output is as follow:
<!DOCTYPE html>
<html>
<head><title>
Home Page
</title><link href="Content/Site.css" rel="stylesheet" type="text/css" />
<script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
</head>
<body>
<div class="page">
<div id="header">
<div id="title">
<h1>My MVC Application</h1>
</div>
<div id="logindisplay">
[ Log On ]
</div>
<div id="menucontainer">
<ul id="menu">
<li>Home</li>
<li>About</li>
</ul>
</div>
</div>
<div id="main">
<div id="menu" class="block">
<form action="/" method="post">
<ul id="menuItems">
<li>Ballot Language</li>
<li>Assisting Absentee Voter</li>
<li>Instructions</li>
<li>Ballot Questions</li>
</ul>
</form>
</div>
<div id="footer">
</div>
</div>
</div>
</body>
</html>
ERROR: When I Click on the link I am getting following error:
Could any one help with it. My intension is to pass menu id to controller and load appropriate usercontrol. I cannot use Javascript or jquery in this project as user might have javascript disabled on machine.
You should remove "controller" word from the links. The url should be:"/Home/Index/1".
To do so, modify the ActionLink code:
Html.ActionLink(item.Description, "Index", "Home", New With {item.ID}, Nothing)%>
Try the link as /Home/Index/1 not /HomeController/Index/1
I am having the following
<form action="<%=Url.Action("PasswordDetails",new{Controller = "User"}) %>" method="post" name="PasswordForm" id="PasswordForm" enctype="multipart/form-data">
However, the $("#PasswordForm").submit(function() {
if (validate())
return true;
else
return false;
}); isn`t being passed through.
What is wrong?
<% using (Html.BeginForm("PasswordDetails", "User", FormMethod.Post, new { id = "PasswordForm" }))
{ %>
Your view page should look like this:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm("PasswordDetails", "User",
FormMethod.Post, new { id = "PasswordForm" }))
{ %>
<input type="password" id="sitepassword" />
<input type="submit" value="Submit" />
<% } %>
</asp:Content>
and your site master like this:
<body>
<div class="page">
<div id="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
<div id="footer">
</div>
</div>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type='text/javascript'>
function validate() {
alert('hello');
}
$(document).ready(function () {
$("#PasswordForm").submit(function () {
if (validate()) return true;
else return false; });
});
</script>
</body>
I am trying to use jQuery ajax with MVC I can get it to post back to the action I want and it returns the ViewData objects with updated Data but never renders the HTML. I have a View which contains some MVC User Controls and I want them to update on a timer.
Here is my View Markup
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/PageWithSummaryViewAndTabs.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="FullCaseTitle" ContentPlaceHolderID="TitleContent" runat="server">
</asp:Content>
<asp:Content ID="FullCaseContent" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
window.setInterval(test, 5000);
function test() {
jQuery.get("/PatientCase/RefreshEPRF", function(response) { });
}
</script>
<div id="loadingDiv" style="display:none">Updating</div>
<input id="refreshPatientCaseIndexButton" type="submit" visible="true" title="refresh" value="Refresh" />
<h2>Full Case</h2>
<div id="EPRFContent">
<%Html.RenderPartial(#"~/Views/PatientCase/SectionEPRF.ascx"); %>
<%Html.RenderPartial(#"~/Views/PatientCase/SectionDrugs.ascx"); %>
</div>
<div id="ImageContent">
<%Html.RenderPartial(#"~/Views/PatientCase/SectionImagery.ascx"); %>
</div>
</asp:Content>
On postback i call a Action Called RefreshEPRF which loads just the required user controls again
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%Html.RenderPartial(#"~/Views/PatientCase/SectionEPRF.ascx"); %>
<%Html.RenderPartial(#"~/Views/PatientCase/SectionDrugs.ascx"); %>
And finaly the marpup in the control
<table id="detailstable">
<tr><td id="detailslablecolumn">Patient Name : </td><td>
<%
foreach (var item in (List<STS_Lite.Models.PatinetCase.EPRFItem>)ViewData["EPRF"])
{
if (item.datumItemId == 46)
{
if (item.Stroke)
{
%>
<img src="/PatientCaseIndex/InkImageData/GetInkImage/<%=ViewData["PatientCaseId"]%>/<%=ViewData["TemplateInstanceId"]%>/<%=item.TemplateItemId %>" />
<%
}
else
{%>
<%=item.Value.ToString()%>
<%}
break;
}
}
%></td></tr><table>
When I step through this code the ViewData in the user control has the new updated values but the page comes back with no new values. I have tried the jQuery.get and ajax but with no luck.
Any help would be great thanks
You have to write your response... try doing this :
jQuery.get("/PatientCase/RefreshEPRF", function(response) {
$("#EPRFContent").html(response) });
The response parameter should contain your view, and doing this simply put its content into your div.
your action Called RefreshEPRF should however return a partial view
I'm getting some very strange behaviour with tinyMCE in an ASP.NET MVC 2 beta app (same happend with MVC 1). I have a view called "edit.aspx" that is called when the user tries to create or edit an entity. The view uses jquery to load tinyMCE where ever it finds textarea's.
Here are my 2 action methods that both call the same "edit.aspx" view
public ActionResult Create()
{
return View("Edit", new FutureEvent());
}
[HttpGet]
public ActionResult Edit(int id)
{
FutureEvent futureEvent = (from fe in adminGalleryRepository.FutureEvents
where fe.ID == id
select fe).FirstOrDefault();
return View("Edit", futureEvent);
}
The "Edit.aspx" view:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage<DomainModel.Entities.FutureEvent>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Future event
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
$(function() {
$("#tabs").tabs();
$('textarea').tinymce({
script_url: '../../Scripts/tiny_mce/tiny_mce.js',
theme: "advanced",
plugins: "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
// Theme options
theme_advanced_buttons1: "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
theme_advanced_buttons2: "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
theme_advanced_buttons3: "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
theme_advanced_buttons4: "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
theme_advanced_toolbar_location: "top",
theme_advanced_toolbar_align: "left",
theme_advanced_statusbar_location: "bottom",
theme_advanced_resizing: true
});
});
</script>
<h2>Future event</h2>
<%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginForm("Edit", "FutureEvents")) {%>
<div id="tabs">
<ul>
<li>Future event</li>
</ul>
<div id="tabs-1">
<%= Html.Hidden("ID") %>
<label class="formLabel" for="Title">Title:
<%= Html.ValidationMessage("Title", "*") %>
<%= Html.TextBox("Title", Model.Title, new { size = "40px" })%>
</label>
<label class="formLabel" for="Info">Info:
<br />
<%= Html.TextArea("Info", Model.Info, 15, 130, null) %>
</label>
<br />
<label class="formLabel" for="WebSite">Web site address:
<%= Html.TextBox("WebSite", Model.WebSite, new { size = "40px" })%>
</label>
</div>
</div>
<div class="clear" ></div>
<div id="footer" style="text-align: left">
<input type="submit" value="Save" />
<%=Html.ActionLink("Back to List", "List") %>
</div>
<% } %>
</asp:Content>
The strange thing is that the create method renders the "edit" view and the tinyMCE edit appears correctly. But when the edit action method renders the "edit" view, the view appears as you would expect - but without the tinyMCE editor.
No errors appear in FireBug, I get exactly the same behaviour in IE.
I also tried removing the $("#tabs").tabs() line but it made no difference.
It could be related to this line:
script_url: '../../Scripts/tiny_mce/tiny_mce.js',
Since the problem is in the Edit view, maybe the extra parameter adds one level to the folder structure.
Why don't you try:
script_url: '/Scripts/tiny_mce/tiny_mce.js',