I have implemented full calendar. It does show as it supposed to, but fetching data goes wrong
.cshtml
<div id='calendar'></div>
.cshtml.cs
public class IndexModel : PageModel
{
public JsonResult OnPost(DateTime start, DateTime end)
{
return new JsonResult(new
{
url = "something",
title = "something else",
start = ConvertToUnixTimestamp(DateTime.Now).ToString(),
end = ConvertToUnixTimestamp(DateTime.Now.AddDays(2)).ToString(),
allDay = false,
backgroundColor = "red",
textColor = "green"
});
}
}
.js
document.addEventListener('DOMContentLoaded', function () {
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
events: {
url: '/Overview/Employee/Index',
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
method: 'POST'
},
plugins: ['dayGrid']
});
calendar.render();
});
When i load the page, i see a request happening, but it returns a 400, bad request. Any idea why?
The request:
I tried your code without 400 error( use #Html.AntiForgeryToken() in the form), but the event is not added to the calendar successfully. It works when I return a List Model, try to use below code to add events:
public class EventModel
{
public int id { get; set; }
public string start { get; set; }
public string end { get; set; }
public string title { get; set; }
public bool allDay { get; set; }
public string url { get; set; }
public string color { get; set; }
public string textColor { get; set; }
}
public class IndexModel : PageModel
{
public JsonResult OnPost(DateTime start, DateTime end)
{
IEnumerable<EventModel> events = new List<EventModel>()
{
new EventModel()
{
url = "something",
title = "something else",
start = (DateTime.Now).ToString(),
end = (DateTime.Now.AddDays(2)).ToString(),
allDay = false,
color = "red",
textColor = "green"
}
};
return new JsonResult(events);
}
}
Results:
Related
Hi I have a graphql Response after Mutating
{{
"carCreate": {
"car": {
"id": "TestId"
}
}
}}
I want to Desealize it I am using The following Code
var graphQlClient = new GraphQLHttpClient(AppConfig.GraphQlUrl, new NewtonsoftJsonSerializer());
I have tried to resolve with the following code
var response = await graphQlClient.SendMutationAsync<CarCreate>(request);
My Created Model is :
public class Car
{
public string Id { get; set; }
}
public class CarCreate
{
public Car Car { get; set; }
}
Your class should be something like this,
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Car
{
public string id { get; set; }
}
public class CarCreate
{
public Car car { get; set; }
}
public class Root
{
public CarCreate carCreate { get; set; }
}
I want to design a response and design it by responses in my asp.net core application. The simlpe response is like following.
public class Response {
public string Status { get; set; } => "Ok";
public BaseReport BaseReport { get;set;}
}
if user sends extra parameters to my service, I want to change my response content dynamically.
public class ReportsController : ControllerBase
{
[HttpGet]
public ActionResult<Response> GetReport(bool isEmployee, bool isFinanace, bool isInformatinTech)
{
// if all parameters fals, return base report.
var report = baseReposrService.Get();
var response = new Response() { BaseReport = report };
if(isEmployee)
{
var ereport = employeeService.Get();
var response = new Response() {
BaseReport = report,
EmployeeReport = ereport
};
}
if(isFinanace)
{
var freport = financeService.Get();
var response = new Response() {
BaseReport = report,
EmployeeReport = freport
};
}
...
...
}
}
the response object is growing by query parameters.
So, is implementing the decorator pattern for this problem right way? Is there any best practice for these type problems?
I tried as below:
public class Response
{
public Response()
{
BaseReport = new BaseReport();
}
public string Status { get; set; } ="Ok";
public BaseReport BaseReport { get; set; }
}
public class BaseReport
{
public string report { get; set; }
public string reportcontent { get; set; }
}
public interface IResponseFactory
{
IResponseFactory Add(string key);
Response Create(string key);
}
public class ResponseFactory1 : IResponseFactory
{
private readonly Dictionary<string, Response> _responsedic = new Dictionary<string, Response>();
public ResponseFactory1()
{
this.Add("Employee").Add("Finanace").Add("InformatinTech");
}
public IResponseFactory Add(string key)
{
var response = new Response();
response.BaseReport.report = key;
response.BaseReport.reportcontent = key + "content";
_responsedic.Add(key, response);
return this;
}
public Response Create(string responsename)
{
if (_responsedic.ContainsKey(responsename))
{
return _responsedic[responsename];
}
else
{
return new Response() { BaseReport = new BaseReport() { report = "basereport",reportcontent= "basereportcontent" } };
}
}
in startup class:
services.AddSingleton<IResponseFactory, ResponseFactory1>();
in controller:
[HttpGet]
public ActionResult<Response> GetReport(string responsetype)
{
var response = _responseFactory.Create(responsetype);
return response;
}
Question :
I am looking for how to let "RenderFragment" can re-Render or re-Binding Resource .
Environment:
VS2019 Preview , .net core 6 preview (I think it is not different with 5)
What I Had Try :
My Razor Component page has a selection and a button ,
Selection will get default resource when page first render ,
Button Click Cvent should change the resource of Selection .
This is What I prefer for correct result
But Actually ,
Resource is not changing when I Using RenderFragment to create component .
Resource is not changing.
I Stop at FabArea.razor to check if button click changed oResource .
Stop at Button Clicked.
I tried to add StateHasChanged() in my code , but it didn't help when oResource has changed .
oResource Changed into B , by Selection's Resource is still same.
In some reason I have to make each "Area" ,"selection" and "button" into different component .
Here is My code .
Parent Component : DyResource.Razor
#page "/DyResource"
<h3>DyResource</h3>
<p>You Just Select : #SelectedValue </p>
<p>isDefault Resource : #(isDefaultResource?"Default Value":"Not Default Value") </p>
<p>Resource[0]: #(oResource.First().id.ToString())</p>
<FabArea Componets="#liComponets"></FabArea>
#code {
public List<iFabComponet> liComponets { get; set; }
public List<Selection> oResource { get; set; }
public string SelectedValue { get; set; }
public bool isDefaultResource { get; set; } = true;
public class Selection
{
public string id { get; set; }
public string text { get; set; }
}
public class iFabComponet
{
public Type Type { get; set; }
public string Row { get; set; }
public string Length { get; set; }
public string Seq { get; set; }
public RenderFragment Control { get; set; }
public Dictionary<string, object> Dic { get; set; }
public object TComponent { get; set; }
}
protected override void OnInitialized()
{
DefaultResource(); //Set oResource
CreateComponent();
}
//Selection Resource When First Render Pages
private void DefaultResource()
{
oResource = new List<Selection>
{
new Selection { id = "A01", text = "A01" },
new Selection { id = "A02", text = "A02" },
new Selection { id = "A03", text = "A03" },
new Selection { id = "A04", text = "A04" },
new Selection { id = "A05", text = "A05" }
};
//StateHasChanged();
}
// Selection Resource When Button Click
private void ChangedResource()
{
oResource = new List<Selection>()
{
new Selection { id = "B01", text = "B01" },
new Selection { id = "B02", text = "B02" },
new Selection { id = "B03", text = "B03" },
new Selection { id = "B04", text = "B04" },
new Selection { id = "B05", text = "B05" }
};
//StateHasChanged();
}
//Create KeyValuePair for Child Components
private void CreateComponent()
{
List<iFabComponet> FCs = new List<iFabComponet>();
var DDLDic = new Dictionary<string, object>();
DDLDic.Add("Label", "Type");
DDLDic.Add("Resource", oResource);
DDLDic.Add("TextField", "id");
DDLDic.Add("ValueField", "text");
DDLDic.Add("Enabled", true);
DDLDic.Add("Id", "ddlType");
DDLDic.Add("Width", "100%");
DDLDic.Add("ResultValueChanged", EventCallback.Factory.Create<System.String>(this, str => TypeSelected(str)));
iFabComponet FirstCom = new iFabComponet() { Type = typeof(FabDDL<string, Selection>), Row = "1", Length = "6", Seq = "1", Dic = DDLDic };
liComponets.Add(FirstCom);
var btnDic = new Dictionary<string, object>();
btnDic.Add("ButtonTitle", "Get Data");
btnDic.Add("isNeedPad", true);
btnDic.Add("PadLength", 4);
btnDic.Add("OnClick", EventCallback.Factory.Create<System.String>(this, str => BtnClick()));
iFabComponet SecCom = new iFabComponet() { Type = typeof(FabButton), Row = "2", Length = "6", Seq = "2", Dic = btnDic };
liComponets.Add(SecCom);
}
//Selection Event
private void TypeSelected(string x)
{
SelectedValue = x;
}
//Button Event
private void BtnClick()
{
if (isDefaultResource)
{
ChangedResource();
isDefaultResource = false;
}
else
{
DefaultResource();
isDefaultResource = true;
}
CreateComponent();
StateHasChanged();
}
}
FabArea.Razor (Area to show and Render Child Component)
#using System.Linq.Expressions
<div class="card">
<div class="card-body">
#foreach (var item in Contents)
{
#item
;
}
</div>
</div>
#code {
[Parameter]
public List<iFabComponet> Componets { get; set; }
public List<RenderFragment> Contents { get; set; }
protected override void OnInitialized()
{
if (Componets.Count() > 0 && Componets != null)
{
CreateFragment();
}
}
public async void CreateFragment()
{
int iComponent = 0;
List<RenderFragment> RFTs = new List<RenderFragment>();
Contents = new List<RenderFragment>();
int iContent = 1;
foreach (var area in Componets)
{
RenderFragment renderFragment = (builder) =>
{
builder.OpenComponent(iComponent, area.Type);
//Using For Checking Resource .
foreach (var item in area.Dic)
{
var q = item.Key;
var w = item.Value;
}
builder.AddMultipleAttributes(iContent, area.Dic);
builder.CloseComponent();
};
Contents.Add(renderFragment);
}
StateHasChanged();
}
}
Here is Selection Component and Button Component .
FabDDL.razor
#typeparam T
#typeparam TResource
<div class="row">
<label class="col-md-2">#Label</label>
<div class="col-md-5">
<select class="selection" id="#id" disabled="#(!Enabled)" #onchange="#(() => ResultValueChanged.InvokeAsync())">
<option></option>
#if (Resource != null)
{
#foreach (var item in Resource)
{
<option value="#(item.GetType().GetProperty(ValueField).GetValue(item))">#(item.GetType().GetProperty(TextField).GetValue(item))</option>
}
}
</select>
</div>
</div>
#code {
[Parameter] public string Label { get; set; }
//[Parameter] public T ResultValue { get; set; }
[Parameter] public List<TResource> Resource { get; set; }
[Parameter] public string DefaultText { get; set; } = "Select an Option";
[Parameter] public string id { get; set; } = "DropDownList" + Guid.NewGuid().ToString();
[Parameter] public string Width { get; set; } = "100 %";
[Parameter] public bool Enabled { get; set; }
[Parameter] public string TextField { get; set; }
[Parameter] public string ValueField { get; set; }
[Parameter] public EventCallback<T> ResultValueChanged { get; set; }
}
<style>
.selection {
width: 100%;
height: 100%;
padding-left: 15px;
}
</style>
FabButton.razor
#if (isNeedPad){<div class="#PadRowClass"></div>}
<div class="col-md-2 pt-3 middle">
<button class="btn btn-info" type="#ButtonType" #onclick="#(() => OnClick.InvokeAsync())">#ButtonTitle</button>
</div>
#code {
[Parameter] public string ButtonTitle { get; set; } = "Click ME!";
[Parameter] public string ButtonType { get; set; } = "button";
[Parameter] public EventCallback<string> OnClick { get; set; }
[Parameter] public bool isNeedPad { get; set; } = false;
[Parameter] public int PadLength { get; set; } = 1;
public string PadRowClass { get; set; }
protected override void OnInitialized()
{
PadRowClass = "col-md-" + PadLength.ToString();
base.OnInitialized();
}
}
I'm trying to figure out how to pull values from a SQL database and display this in a razor view.
I have the following class using Entity Framework (I believe)
public class EventLog
{
[Key]
public int Id { get; set; }
public int EventId { get; set; }
public int MaxDelegates { get; set; }
public string Code { get; set; }
public DateTime End { get; set; }
public string Title { get; set; }
}
And I want to map title to DBTitle in the following model:
public class CourseDetailVM : CourseDetailSummaryVM
{
public EventLog DBTitle { get; set; }
}
I then want to see this in the following view:
#using TSW.Web.Helpers
#model TSW.Web.ViewModels.CourseDetailVM
#{
Layout = "~/Views/_Master.cshtml";
}
#Model.DBTitle.Title;
I have the following controller already in place (sorry for the length I plan to reduce this down):
public class CourseDetailController : BaseRenderController<CourseDetailPageDT>
{
private readonly ISitePageFactory _pageFactory = null;
private readonly IEventService _eventService = null;
public CourseDetailController(IEventService eventService, ISitePageFactory pageFactory)
{
_pageFactory = pageFactory;
_eventService = eventService;
}
public async Task<ActionResult> CourseDetail()
{
var homepage = _pageFactory.GetCurrentHomepage();
var model = Mapper.Map<CourseDetailVM>(CurrentContent);
model.Email = homepage.ContactEmail;
model.PhoneNumber = homepage.HeaderPhoneNumber;
model.InnerPageHeader.ShowHeading = true;
model.InnerPageHeader.Title = model.PageTitle;
if (model.Categories.Count == 1)
{
var categoryTagId = model.Categories.First().Id;
var contentTypeAlias = DocumentTypeHelper.GetDocumentTypeAlias<CourseListingPageDT>();
var courseCategoryPage = Umbraco.TypedContentAtXPath($"//{contentTypeAlias}")
.FirstOrDefault(x => x.GetPropertyValue<int>(Constants.DocumentTypes.CourseListingPage.Category) == categoryTagId);
if (courseCategoryPage != null)
{
model.InnerPageHeader.BackLink = Mapper.Map<LinkItem>(courseCategoryPage.Id);
}
}
try
{
model.Events = await _eventService.GetEventsForCourse(CurrentContent.AdministrateId);
}
catch (Exception ex)
{
model.Events = new StaticPagedList<Event>(Enumerable.Empty<Event>(), 1, 1, 0);
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
if (CurrentContent.Graphic != 0)
{
model.InnerPageHeader.Graphic = Mapper.Map<CtaItem>(CurrentContent.Graphic);
}
return View(model);
}
}
I've tried every suggestion I can google to add the mapping in the controlling but can't get my head around this simple function of pulling the value from a SQL database into the razor view.
Could anyone help me out?
I have a set of questions the user can choose from and some of those questions have a secondary list of options to choose from. My goal is to have a drop down list and if you pick one of the options that has items in its SecondaryChoiceList then a second list would appear below the initial dropdown and all of this would be strongly typed and bound to the model upon submission.
I can get the initial list to appear by saying:
#Html.DropDownListFor( x => x.SelectedChoiceId, new SelectList(Model.Choices, "Id", "Name"))
But that has no hooks to the secondary list and I am completely lost as to how I would tie that secondary list back to the model that is returned when I submit the form.
Here's my view model:
public class ExampleViewModel
{
public List<Choice> ChoiceList { get; set; }
public int SelectedChoiceId { get; set; }
public int SelectedAffiliateId { get; set; }
}
Here is what a Choice looks like:
public class Choice
{
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<SecondaryChoice> SecondaryChoiceList { get; set; }
public Choice()
{
SecondaryChoiceList = new List<SecondaryChoice>();
}
}
And here is my SecondaryChoice object:
public class EligibleAffiliate
{
public int Id { get; set; }
public int EligibilityChoiceId { get; set; }
public string Name { get; set; }
}
If there is anything that I can clear up let me know.
I have tried to keep it as simple as possible.
So, a sample model is given below:
namespace StackOverflow.Models
{
public class Choice
{
public int Id { get; set; }
public string Name { get; set; }
public Choice()
{
Id = 0;
}
public Choice(int id, string name)
{
Id = id;
Name = name;
}
}
}
namespace StackOverflow.Models
{
public class ExampleViewModel
{
public List<Choice> PrimaryChoiceList { get; set; }
public List<Choice> SecondaryChoiceList { get; set; }
public int SelectedChoiceId { get; set; }
public int SelectedAffiliateId { get; set; }
public ExampleViewModel()
{
SelectedChoiceId = 0;
SelectedAffiliateId = 0;
PrimaryChoiceList = new List<Choice>()
{
new Choice(1, "How are you?"),
new Choice(2, "How is the weahter?"),
new Choice(3, "What have you been doing so far?"),
new Choice(4, "What's up man?"),
new Choice(5, "Any news?"),
new Choice(5, "Bla bla bla")
};
SecondaryChoiceList = new List<Choice>()
{
new Choice(1, "How are you dear?"),
new Choice(2, "How is the weahter?"),
new Choice(3, "What have you been doing so far dear?"),
new Choice(4, "What's up man?"),
new Choice(5, "Any romantic news?")
};
}
}
}
Sample controller:
namespace StackOverFlow.Controllers
{
public class SOController : Controller
{
public static ExampleViewModel evm = new ExampleViewModel();
public ActionResult Index()
{
return View(evm);
}
public ActionResult SetSelection(int id)
{
evm.SelectedChoiceId = id;
if (evm.PrimaryChoiceList.Count() > 0)
{
Choice selection = evm.PrimaryChoiceList.ElementAt(id-1);
Choice affiliate = (Choice)evm.SecondaryChoiceList.FirstOrDefault(x => x.Name == selection.Name);
if (affiliate != null)
{
return Content("show");
}
else
{
return Content("hide");
}
}
else
{
return Content("hide");
}
}
}
}
And the web page:
#using StackOverflow2.Models;
#model ExampleViewModel
<script src="#Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
#{
ViewBag.Title = "Stackoverflow Sample";
}
<h2>Index</h2>
<script type="text/javascript">
// Get the selection and make Ajax Request to the controller, action: SetSelection,
// which in turn may decide whetger you must show or hide the control
function updateSeconadryQuestion(id) {
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
if (xmlhttp.responseText == 'show')
$('#SecondaryQuestionDropBoxId').show();
else
$('#SecondaryQuestionDropBoxId').hide();
}
}
xmlhttp.open("GET", "/SO/SetSelection?id=" + id, true);
xmlhttp.send();
}
</script>
#Html.DropDownListFor(x => x.SelectedChoiceId, new SelectList(Model.PrimaryChoiceList, "Id", "Name", "Value"), new { id = "PrimaryQuestionDropBoxId", onchange = "updateSeconadryQuestion(value);" })
<div id="SeconadryQuestionDivId">
#Html.DropDownListFor(x => x.SelectedAffiliateId, new SelectList(Model.SecondaryChoiceList, "Id", "Name"), new { id = "SecondaryQuestionDropBoxId" })
</div>