I am implementing the full calender in jquery in my asp.net mvc application by referring from here
but as per this blog it should be render the events on dates given in the controller action. but it is does not. i followed exact same steps . i checked many times there is no mistake. so why should be this happening? please guide me
Edited
Controller:
public ActionResult CalendarData()
{
IList<CalendarDTO> tasksList = new List<CalendarDTO>();
tasksList.Add(new CalendarDTO
{
id = 1,
title = "Google search",
start = ToUnixTimespan(DateTime.Now),
end = ToUnixTimespan(DateTime.Now.AddHours(4)),
url = "www.google.com"
});
tasksList.Add(new CalendarDTO
{
id = 1,
title = "Bing search",
start = ToUnixTimespan(DateTime.Now.AddDays(1)),
end = ToUnixTimespan(DateTime.Now.AddDays(1).AddHours(4)),
url = "www.bing.com"
});
return Json(tasksList);
}
private long ToUnixTimespan(DateTime date)
{
TimeSpan tspan = date.ToUniversalTime().Subtract(
new DateTime(1970, 1, 1, 0, 0, 0));
return (long)Math.Truncate(tspan.TotalSeconds);
}
Added Class
public class CalendarDTO
{
public int id { get; set; }
public string title { get; set; }
public long start { get; set; }
public long end { get; set; }
public string url { get; set; }
}
Site.Master
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
<link href="../../Content/fullcalendar.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/fullcalendar.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-1.3.2.js" type="text/javascript"></script>
View Page I used Index page which given as default
$(document).ready(function() {
$('#calendar').fullCalendar({
events: "/Home/CalendarData"
});
});
And added div with id "calender".
-----------------------------------------------------------------------------------------
Edited Quetion 2
As you can see above my method returning the Json out put. but I am getting error as:
This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.
So I just add the parameter as JsonRequestBehavior.AllowGet to Json() . but it is asking for download the json output file. rather than this it must be redirect to view as usual , right ? why should this is happening?
Probably a stupid question but did you include jquery-1.3.2.js and fullcalendar.js in your site? Remember that with the default ASP.NET MVC 2.0 project template, only jquery-1.4.1.js is included in the Scripts folder. Also I would recommend you downloading the latest versions of jquery and the fullCalendar plugin.
Also here's another gotcha when returning JSON in the CalendarData action:
return Json(tasksList, JsonRequestBehavior.AllowGet);
Contrary to ASP.NET MVC 1.0, in ASP.NET MVC 2.0 the JsonRequestBehavior.AllowGet is necessary if you want this action to be accessible over GET which is what I think the calendar plugin is doing.
Of course you would have seen this error if you used FireBug to analyze the AJAX request/response data.
Related
I'm new to Blazor and bUnit. I have component that renders an edit form and I get the values for the form in my OnInitializedAsync event.
I'm having trouble working out how to use cut.WaitForState() or cut.WaitForAssertion().
Here's my razor code:
#page "/{AppId:guid}/app-settings-edit"
<section class="app-settings-edit">
<h1 class="page-title">Application Settings</h1>
#if (InitializedComplete)
{
<p>Hello World</p>
...
And my code behind:
public partial class AppSettingsEdit
{
protected bool InitializedComplete;
[Parameter]
public Guid AppId { get; set; }
[ValidateComplexType]
public AppSettings AppSettings { get; set; } = new AppSettings();
[Inject]
public IAppSettingsDataService AppSettingsDataService { get; set; }
protected override async Task OnInitializedAsync()
{
AppSettings = await AppSettingsDataService.Get(AppId);
InitializedComplete = true;
}
...
And here's my Test:
[Fact]
public void MyFact()
{
Services.AddSingleton<IAppSettingsDataService, MockAppSettingsDataService>(x => new MockAppSettingsDataService(x.GetRequiredService<HttpClient>()));
var cut = RenderComponent<AppSettingsEdit>(parameters => parameters
.Add(p => p.AppId, Guid.Parse("55E5097B-B56A-40D7-8A02-A5B94AAAD6E1"))
);
Assert.NotNull(cut.Instance.AppSettingsDataService);
cut.WaitForState(() => cut.Find("p").TextContent == "Hello World", new TimeSpan(0, 0, 5));
cut.MarkupMatches("<p>Hello World</p>");
}
When I debug the test, I can see the OnInitializedAsync firing, however my markup never changes to include 'Hello World' and the WaitForState() command fails.
Are you certain that the task returned from your AppSettingsDataService.Get() method ever completes?
I would make sure that the task returned from AppSettingsDataService.Get() is already completed, otherwise you need to a way to complete the task after the component is rendered. There are many ways to do this, it all depends on how your mock is implemented.
As for your WaitFor, you can just use the WaitForAssertion method in this case, i.e.: cut.WaitForAssertion(() => cut.MarkupMatches("<p>Hello World</p>"));
A little background:
The WaitFor* methods are used when the component under test is being rendered asynchronously, since the test, running in a different thread, doesn't know when that will happen.
In general, you should never need to set a custom timeout, the default is 1 second, but the WaitFor* methods will retry the assertion/predicate every time a renderer happens. Its only when the thing that triggers the rendering will take more than one second, e.g. if you are using bUnit to perform end-2-end testing and e.g. pulling data from a real web service.
I want to make the validation element launched during the filling out of the form field.
If I create a simple MVC application that contains simple model:
public class SimpleModel
{
[RegularExpression("[A-Z][a-z]+")]
[Required]
public string FirstName { get; set; }
[RegularExpression("[A-Z][a-z]+")]
[Required]
public string LastName { get; set; }
}
For this model I created the corresponding Controller and View(Razor using template Edit) for this model.
At this time, everything worked as it should. But only when clicking on the Save button(submitting the form), not during filling the field. That's why I wanted to implement action onkeyup of the validator in script section:
#section Scripts
{
#Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$('form').validate(
{
onkeyup: function (element)
{
$.validator.unobtrusive.parseElement(element, true);
}
});
</script>
}
But it does not work and the original validation also does not work(now if i click the save, the form is submited as valid altrough fields are not filled or are filled in incorrectly).
And the original validation does not work if I use only
$('form').validate();
or
$('form').valid();
command.
And maybe it is interesting that
$('form').valid();
returns true although form is not filled and thus is not valid.
The solution is quite easy. Just realize, that validation using a script it is possible after initialization of the unobtrusive. This initialization is performed by registering new jquery ready function - in jquery.validate.unobtrusive.js at the bottom you find this code:
$(function () {
$jQval.unobtrusive.parse(document);
});
Therefore, if it has to carry out additional initialization, so they must create own ready function and insert the necessary code.
For the purpose referred to in the question above, you can use this code:
#section Scripts
{
#Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$(function ()
{
var validator = $('form').validate();
validator.form();
});
</script>
}
I am new to asp and have written a project that will connect to a database, retrieve number records and then present those records to the user in a paginated table. The user can then click on a record to edit, make the edit, save the change and be returned to the original table view. The update page is strongly typed.
I am struggling to keep track of which pagination page was last viewed and then navigating back to it. I.e. if the user is on page 5 of 10, they then update a record in from page 5, when the edit is saved the table is shown again but it has gone back to page 1. What is the best method to keep track of the last pagination page?
Any help appreciated.
Chris
Typically I will pass the page number in form submissions, and then when redirecting after save, pass it as part of the query string, so your action can provide the correct page of data back to the view.
This also lets users bookmark specific pages, which can be useful, or even just refresh the current page if necessary, without losing their place.
You may also need to pass sort information, if that feature is available.
I've found it's nice to save it in viewstate:
ex:
ViewState("paging") = 1
In the end I used a session cookie on the server to keep track of the pages shown and last search. This meant I could navigate between pages/controller and keep track of which page was showing in each and didn't need to pass lots of parameters around. I added a new class:
public class SearchModel
{
public string TargetController { get; set; }
public string TargetMethod { get; set; }
public string OriginalSearchCriteria { get; set; }
public string NewSearchCriteria { get; set; }
public int Page { get; set; }
public void SetCriteria(String newCriteria, int pageIn)
{
if (!newCriteria.Equals(OriginalSearchCriteria))
{
OriginalSearchCriteria = newCriteria;
Page = 1;
}
else
{
Page = pageIn;
}
}
public void SetPage(int newPage)
{
if (newPage != 0)
Page = newPage;
}
}
In the controller I just added:
private SearchModel GetSearch()
{
SearchModel search = (SearchModel)Session["CgRefCodeSearch"];
if (search == null)
{
search = new SearchModel();
search.Page = 1;
search.OriginalSearchCriteria = "";
Session["CgRefCodeSearch"] = search;
}
return search;
}
On each function in the controller I could then reference this:
GetSearch().SetPage(page);
CurrentPage = GetSearch().Page etc...
This was based on stuff I read in this Pro ASP.NET MVC 3 Framework, Third Edition by Adam Freeman; Steven Sanderson. Its really simple but works OK.
I'm working on ASP.NET Dynamic Data Entities Web Application with Scaffolding.
The scaffolding, Page Templates viz. Insert. Edit, Details, List, Entity Template Web User Controls all are working fine.
BUT my problem is, I want to Validate Primary Key during Insertion operation.
Say now If i left the Primary Key Field blank it validates with required field validator, same way I want to validate for Duplicate Entry in Primary Key Field.
I even tried Custom Validator in Data Model, but unable to find proper solution for the same.
Can anyone help me please
As you told me you are using MVC, We have an Remote Validation in MVC which we can apply in our Model Class Properties.
Let me show you how:
[Required]
[Remote("IsUserIDExist", "Account", ErrorMessage = "User ID Already Exist")]
[Display(Name = "Enter User ID")]
public string User_username { get; set; }
This is my Property which is in Account.cs Model Class.
IsUserIDExist is the Action in Account Controller.
Now let me show you IsUserIDExist Action.
public ActionResult IsDomainIDExist(string User_username)
{
var users = from s in db.CreateUsers
where s.User_username == User_username
select s;
if (users != null)
{
if (users.Count() != 0)
{
return Json(false, JsonRequestBehavior.AllowGet);
}
else
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}
else
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}
This Code speaks it self.I guess there is no need for any explanation.
Last but not least.
You need to add these javascript files to work with Remote Validation
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"> </script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.json-2.2.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
Hope this is what you were looking for.
I am trying to dynamically generate JavaScript with just a URL get request.
I have accomplished this with asp.net MVC by just returning a string from the action and writing the script tag like this.
<script type='text/javascript' src='script/get/123'></script>
The problem is I need to accomplish the same type of dynamically generated script from a asp.net web forms project.
How would I return dynamiccally generated string with a GET request to a page (or web service) in a asp.net Web Forms project?
You could write a generic handler:
public class CustomJsHandler : System.Web.IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/javascript";
context.Response.Write("alert('Hello world');");
}
public bool IsReusable
{
get
{
return true;
}
}
}
and then specify the address of this handler:
<script type="text/javascript" src="/customjshandler.ashx"></script>