Send JSON from AngularJS to ASP.Net Core
I found that is anulgarjs and ASP.net Core are working very well together!
In the client side:
var Item = {};
Item.Id = 1;
Item.Name = "Test";
$http.post('/Items/Create', Item);
In the server side controller, then the JSON object can automatic parsed.
You just need to use [FromBody], I found without that, the parameter object will be always null. I think you need to tell the MVC, where is the JSON content:
[HttpPost]
[Authorize]
public async Task
That's easy and clean!
ASP.Net Core MVC Validations are not working , after added a click event in the submit button
I added a click event in the submit button to display a confirmation message:
$("#btnSubmit").click(function () {
alert("An event is clicked");
});
That broke all client-side validations of ASP.Net Core MVC, however, the server side validations is still working. You cannot submit an event with invalid data, but it won't display the errors messages.
I tried to add the javascript code in the submit event in form instead of the click event of the event, that is not working too. Even I added another invisible submit button, after the confirmation message is displayed, then the code will trigger the click event of that invisible submit button. That is still not working.
I randomly placed the script tags. I found the script file which contains the click event needs to place after the validation scripts:
<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script><script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
DateTime in ASP.Net Core at Azure
I did the ASP.Net Core MVC web app in Azure. I found all DateTime values are displayed in US format. Even I set in Startup.cs set DefaultRequestCulture to be en-au. It does not work. Finally, I used to use DisplayFormat attribute in The object.
Code
[DisplayFormat(DataFormatString = @"{0:dd\/MM\/yyyy}", ApplyFormatInEditMode = true)] | |
public DateTime? DateTime { get; set; } |
That works in display form. Moreover, the input field will display value in this format.
Nest Objects in View Model in ASP.Net MVC Core
I started to use ASP.Net Core in our web development. The first challenge I faced is about the nested View Model.
I have not used much in nested view model in the previous of MVC,
I got the View Models like this:
Code
public Order{ | |
public int Id {get;set;} | |
public Customer Customer {get;set;} | |
} | |
| |
public Customer{ | |
public string FirstName {get;set;} | |
public string LastName {get;set;} | |
} |
If I wish to do the binding in the razor html
You have to add the bind in the controller
Code
public async Task add the binding in the Customer class too | |
|
mc_code_odd">[Bind("FirstName,LastName")]
public Customer{
public string FirstName {get;set;}
public string LastName {get;set;}
}
[Xamarin]Get the Screen Height and convert for Height Request
That is impossible to detect screen size in the Xamarin Form. You have to do in the native Android code.l suggest to do in MainActivity and then put in the static variable in the Forms PCL project like this:
Android.Util.DisplayMetrics displayMetrics = new Android.Util.DisplayMetrics();
this.WindowManager.DefaultDisplay.GetMetrics(displayMetrics);
App.Height=(int)(displayMetrics.HeightPixels/displayMetrics.Density);
Also, the unit of HeightRequest is device-independent units. But I found if you use HeightPixel divide by Density of the screen, then it will convert into the unit for HeightRequest or WidthRequest