I had my ASP.NET MVC actions written like this:
//
// GET: /TaxStatements/CalculateTax/{prettyId}
public ActionResult CalculateTax(int prettyId)
{
if (prettyId == 0)
return Json(true, JsonRequestBehavior.AllowGet);
TaxStatement selected = _repository.Load(prettyId);
return Json(selected.calculateTax, JsonRequestBehavior.AllowGet); // calculateTax is of type bool
}
I had problems with this because when using it in jquery functions I had all sorts of error, mostly toLowerCase() function failing.
So I had to change the actions in a way that they return bool as string (calling ToString() on bool values), so that thay return true or false (in the qoutes) but I kinda don't like it.
How do others handle such a case?
I would use anonymous object (remember that JSON is a key/value pairs):
public ActionResult CalculateTax(int prettyId)
{
if (prettyId == 0)
{
return Json(
new { isCalculateTax = true },
JsonRequestBehavior.AllowGet
);
}
var selected = _repository.Load(prettyId);
return Json(
new { isCalculateTax = selected.calculateTax },
JsonRequestBehavior.AllowGet
);
}
And then:
success: function(result) {
if (result.isCalculateTax) {
...
}
}
Remark: if the selected.calculateTax property is boolean the .NET naming convention would be to call it IsCalculateTax.
Related
When I PUT to my controller for an update, I can use code like the following to ensure that only those properties that are specified in the object are updated. In other words, if I had a ControlLinePointDto object with properties ID, X, Y and Z, the following would only update property X
JSON
{
"key" : 5,
"values" : {
"X": 1234
}
}
Controller
[HttpPut]
public async Task<IActionResult> PutControlLinePoint(int key, string values)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
int id = key;
ControlLinePoint controlLinePoint = _context.ControlLinePoint.First(x => x.ControlLinePointId == key);
JsonConvert.PopulateObject(values, controlLinePoint);
if (id != controlLinePoint.ControlLinePointId) return BadRequest();
_context.Entry(controlLinePoint).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ControlLinePointExists(id)) return NotFound();
else throw;
}
return NoContent();
}
Now I want to do the same for an array of controllinepoints. I could create an object that was simply [{"key":5, "values":{"X": 1234}}], and deserialize it - then utilize my code per aboce, but this is starting to get pretty complex. Is there a better way?
The best solution I could come up with involves reading the request as a JArray rather than a List. I can then recurse and get the ID for each object. Get the object from the database and PopulateObject to just update the relevant properties. Looks something like this;
[HttpPut("UpdateControlLinePointSet")]
public async Task<IActionResult> UpdateControlLinePointSet([FromBody] JArray pointSetJson)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
foreach (JToken p in pointSetJson)
{
ControlLinePoint clp = _context.ControlLinePoint.First(x => x.ControlLinePointId == (int)p["ControlLinePointId"]);
JsonConvert.PopulateObject(p.ToString(), clp);
_context.Entry(clp).State = EntityState.Modified;
}
await _context.SaveChangesAsync();
return NoContent();
}
I have a problem, I need to send data from my Angular to my ASP.NET Core server. Here is controller:
[HttpPut]
public IActionResult setCoupon(int id, string CouponCode, int DiscountPercent)
{
try
{
var coupon = new Coupon()
{
Id = id,
CouponCode = CouponCode,
DiscountPercent = DiscountPercent
};
return Ok(coupon);
}
catch (Exception)
{
return BadRequest("Wystąpił błąd");
}
}
Here is factory from ngResource (getCoupon is working):
app.factory('couponApi',
function($resource) {
return $resource("/coupon/setCoupon",
{},
{
getCoupon: {
method: "GET",
isArray: false
},
putCoupon: {
method: "PUT",
isArray: false,
}
});
});
Here is usage of factory:
$scope.addCouponCode = function(coupon) {
couponApi.putCoupon(coupon);
};
When i debug my asp.net server i found my params null or 0. I have the same problem on restangular library.
I also try this way to write controller method
[HttpPut]
public IActionResult setCoupon(Coupon coupon)
{
try
{
return Ok(coupon);
}
catch (Exception)
{
return BadRequest("Wystąpił błąd");
}
}
My json which I try to send is this
{"id":1,"couponCode":"abc","discountPercent":10}
and my Echo method send me this:
{"id":0,"couponCode":null,"discountPercent":0}
Update
Apparently in asp.net core, method need to have attribute[FromBody]
[HttpPut]
public IActionResult setCoupon([FromBody] Coupon coupon)
{
try
{
return Ok(coupon);
}
catch (Exception)
{
return BadRequest(new {errorMessage = "Wystąpił błąd"});
}
}
As Aldo says in the comments. The answer is C# expects case-sensitive matching of the json data. So:
{"id":1,"couponCode":"abc","discountPercent":10}
needs to be:
{"id":1,"CouponCode":"abc","DiscountPercent":10}
You were getting a 0 for 'discountPercent' because that is the default value of the unmatched int, whereas null is the default for a unmatched string, hence Echo returns:
{"id":0,"couponCode":null,"discountPercent":0}
I was wondering if it was possible to have more than 1 action in the link. For example, If I wanted to have multiple links such as:
http://www.mywebsite.com/(CONTROLLER)/(ID)/(ACTION)
[http://]www.mywebsite.com/user/Micheal/EditMovies
[http://]www.mywebsite.com/user/Micheal/EditFavorites
Is there some sort of way to do this? If not, do I have to specify multiple id's in the function and then use a case to determine which page they are going to be sent to?
In my UserController.cs I have:
public ActionResult Index(string username)
{
if (username != null)
{
try
{
var userid = (Membership.GetUser(username, false).ProviderUserKey);
Users user = entity.User.Find(userid);
return View(user);
}
catch (Exception e)
{
}
}
return RedirectToAction("", "Home");
}
In my Routes I have:
routes.MapRoute(
name: "User",
url: "User/{username}",
defaults: new { controller = "User", action = "Index" }
);
What I'm trying to make it do is have additional functions for second actions so I can do something like:
User/{username}/{actionsAdditional}
And In my UserController I can put more actions which will leader to the second action actionsAdditional
public ActionResult Index(string username)
{
if (username != null)
{
try
{
var userid = (Membership.GetUser(username, false).ProviderUserKey);
Users user = entity.User.Find(userid);
return View(user);
}
catch (Exception e)
{
}
}
return RedirectToAction("", "Home");
}
public ActionResult EditFavorites()
{
//DoStuff
}
You could do this multiple ways, here's just one:
Set up a route to handle this:
routes.MapRoute("UserEditsThings",
"user/{id}/edit/{thingToEdit}",
new { controller = "UserController", action="Edit" },
new { thingToEdit = ValidThingsToEditConstraint() }
);
Then your action in the User Controller should look like this:
public ActionResult Edit(ThingToEdit thingToEdit) {
ThingToEditViewModel viewModel = new ThingToEditViewModel(thingToEdit);
return View(viewModel);
}
The RouteConstraint is what would take their input (the thingToEdit) and make sure it was valid (you could do this in a few places - like in a Custom ModelBinder):
public class ValidThingsToEditConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
//simplistic implementation simply to show what's possible.
return values['thingToEdit'] == "Favorites" || values['thingToEdit'] == "Movies";
}
}
Now, that way, you can have one method to Edit both Movies and Favorites, and you simply add a parameter to show what 'type' of thing they're editing.
If you wanted to keep your current route, you should be able to do the following:
routes.MapRoute("UserEditsThings",
"user/{id}/edit{thingToEdit}",
new { controller = "UserController", action="Edit" },
new { thingToEdit = ValidThingsToEditConstraint() }
);
I've been away from ASP.NET MVC for about 7 months, so this could be a little rusty. It has not been tested for syntax errors and bits of python may shine through. It should get you there, though.
I'm using code below to get JSON data:
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult getBranchViaJson()
{
Json(getBranchList(AppSession.BranchID.Value));
}
private object getBranchList(int n)
{
var mybranchList = from p in getBranchs(n)
select new { p.Code, p.Name };
return mybranchList.ToArray();
}
Client side retain value :
[{"Code":000,"Name":"Milan"},
{"Code":001,"Name":"Istanbul"},
{"Code":002,"Name":"Baku"},]
But I want to get like this:
[{000:"Milan"},{001:"Istanbul"},{002:"Baku"}]
What is the best way to do this?
First things first:
[{000:"Milan"},{001:"Istanbul"},{002:"Baku"}]
is invalid JSON. Properties must be quoted like so:
[{"000":"Milan"},{"001":"Istanbul"},{"002":"Baku"}]
In order to achieve this output you could use a Dictionary<string, string> that the JavaScriptSerializer will serialize to the desired output. So simply call the ToDictionary extension method on your model in order to convert it to a dictionary:
Like that:
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult getBranchViaJson()
{
var branches =
from p in getBranchs(AppSession.BranchID.Value)
select new { p.Code, p.Name };
var model = branches.ToDictionary(x => x.Code, x => x.Name);
return Json(new[] { model }, JsonRequestBehavior.AllowGet);
}
or if you want to keep your private method which returns an object you could make it return a dictionary:
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult getBranchViaJson()
{
return Json(getBranchList(AppSession.BranchID.Value), JsonRequestBehavior.AllowGet);
}
private object getBranchList(int n)
{
var mybranchList = from p in getBranchs(n)
select new { p.Code, p.Name };
return new[] { mybranchList.ToDictionary(x => x.Code, x => x.Name) };
}
Notice that I used new[] { model }. That's because otherwise the JavaScriptSerializer won't produce a javascript array as required but a simple javascript object.
Remark: notice that I have added JsonRequestBehavior.AllowGet so that this controller action can be consumed with a GET request which is disabled by default for actions returning JSON responses.
First of all it might be worth looking at this question:
How can I cache objects in ASP.NET MVC?
There some pseudo code that almost does what i want:
public class CacheExtensions
{
public static T GetOrStore<T>(this Cache cache, string key, Func<T> generator)
{
var result = cache[key];
if(result == null)
{
result = generator();
cache[key] = result;
}
return (T)result;
}
}
However, what I'd really like to do, is auto-generate the "key" from the generator. I figure i need to change the method signature to:
public static T GetOrStore<T>(this Cache cache,
System.Linq.Expressions.Expression<Func<T>> generator)
I want to use the method name, but also any parameters and their values to generate the key. I can get the method body from the expression, and the paramter names (sort of), but I have no idea how to get the paramter values...?
Or am I going about this the wrong way? Any ideas much appreciated.
Here's how I did it:
public static class ICacheExtensions
{
public static T GetOrAdd<T>(this ICache cache, Expression<Func<T>> getterExp)
{
var key = BuildCacheKey<T>(getterExp);
return cache.GetOrAdd(key, () => getterExp.Compile().Invoke());
}
private static string BuildCacheKey<T>(Expression<Func<T>> getterExp)
{
var body = getterExp.Body;
var methodCall = body as MethodCallExpression;
if (methodCall == null)
{
throw new NotSupportedException("The getterExp must be a MethodCallExpression");
}
var typeName = methodCall.Method.DeclaringType.FullName;
var methodName = methodCall.Method.Name;
var arguments = methodCall.Arguments
.Select(a => ExpressionHelper.Evaluate(a))
.ToArray();
return String.Format("{0}_{1}_{2}",
typeName,
methodName,
String.Join("|", arguments));
}
}
with this helper to evaluate nodes of an expression tree:
internal static class ExpressionHelper
{
public static object Evaluate(Expression e)
{
Type type = e.Type;
if (e.NodeType == ExpressionType.Convert)
{
var u = (UnaryExpression)e;
if (TypeHelper.GetNonNullableType(u.Operand.Type) == TypeHelper.GetNonNullableType(type))
{
e = ((UnaryExpression)e).Operand;
}
}
if (e.NodeType == ExpressionType.Constant)
{
if (e.Type == type)
{
return ((ConstantExpression)e).Value;
}
else if (TypeHelper.GetNonNullableType(e.Type) == TypeHelper.GetNonNullableType(type))
{
return ((ConstantExpression)e).Value;
}
}
var me = e as MemberExpression;
if (me != null)
{
var ce = me.Expression as ConstantExpression;
if (ce != null)
{
return me.Member.GetValue(ce.Value);
}
}
if (type.IsValueType)
{
e = Expression.Convert(e, typeof(object));
}
Expression<Func<object>> lambda = Expression.Lambda<Func<object>>(e);
Func<object> fn = lambda.Compile();
return fn();
}
}
When calling a function that produces a collection i want to cache i pass all my function's parameters and function name to the cache function which creates a key from it.
All my classes implement an interface that has and ID field so i can use it in my cache keys.
I'm sure there's a nicer way but somehow i gotta sleep at times too.
I also pass 1 or more keywords that i can use to invalidate related collections.