1

This is action from my controller

[HttpPost]
[AjaxAction]
public ActionResult Registration(RegisterUserModel registerUser)
{
    var data;

    if (ModelState.IsValid)
    {
        if (!IsUserExist(registerUser.Email))
        {    

            var crypto = new SimpleCrypto.PBKDF2();

            var encrpPass = crypto.Compute(registerUser.Password);

            var newUser = _db.Users.Create();

            newUser.Name = registerUser.Name;
            newUser.Email = registerUser.Email;
            newUser.Type = UserType.User.ToString();

            newUser.Password = encrpPass;
            newUser.PasswordSalt = crypto.Salt;

            _db.Users.Add(newUser);
            _db.SaveChanges();

            data = new { status = "OK", message = "Success" };

        }
        else
        {

            data = new { status = "ERROR", message = "User already exists" };
        }
    }
    else
    {

        data = new { status = "ERROR", message = "Data is incorrect" };
    }
    return Json(data, JsonRequestBehavior.AllowGet);
}

but I don't know how to initialize data variable in the right way, because I need to set different values in different cases. What is the right way to do that?

4 Answers 4

2

I usually use multiple return statements to avoid having to declare an object like

if(something){
return Json(new{status = "status 1", message = "message1"})
}
else{
return Json(new{status = "status 2", message = "message2"})
}
Sign up to request clarification or add additional context in comments.

2 Comments

just an FYI. the else statement isn't needed if can be written if (true) { return 'whatever' } return 'whatever';
@Fran great notice, but actually I don't like to have multiple returns in my actions, I prefer to have the one. Maybe I'm not right.
2

you can try this:

var data = new object();

3 Comments

how do you populate the status and message property with an object
As far, as I know, there are no public properties available with System.Object
And also, the code would not compile if you initialize data as object and then assign it an anonymous object with two properties. Dot Net creates types for anonymous objects on the fly
2

Here is one of the options

[HttpPost]
[AjaxAction]
public ActionResult Registration(RegisterUserModel registerUser)
{
    JsonResult data;

    if (ModelState.IsValid)
    {
        if (!IsUserExist(registerUser.Email))
        {    

            var crypto = new SimpleCrypto.PBKDF2();

            var encrpPass = crypto.Compute(registerUser.Password);

            var newUser = _db.Users.Create();

            newUser.Name = registerUser.Name;
            newUser.Email = registerUser.Email;
            newUser.Type = UserType.User.ToString();

            newUser.Password = encrpPass;
            newUser.PasswordSalt = crypto.Salt;

            _db.Users.Add(newUser);
            _db.SaveChanges();

            data = Json(new { status = "OK", message = "Success" }, JsonRequestBehavior.AllowGet);

        }
        else
        {

            data = Json(new { status = "ERROR", message = "User already exists"}, JsonRequestBehavior.AllowGet);
        }
    }
    else
    {

        data = Json(new { status = "ERROR", message = "Data is incorrect" }, JsonRequestBehavior.AllowGet);
    }
    return data;
}

1 Comment

Thanks a lot, interesting idea.
1

You can use the dynamic keyword.

dynamic data;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.