Hi All,
I would appreciate a lot if someone could help me to clear my doubt on below scenario.
I am working on a project where I am using web forms. Its a web application. I have a created entity classes which represent my database. For example I have two entity classes as below which is same as my database
public class User
{
public int userid {get; set;}
public string name {get; set;}
public string email {get; set;}
public string pwd {get; set;}
public int isUserActive {get; set;}
public Role userRole {get; set;}
public Language language{get; set;}
}
public class Role
{
public int roleId {get; set;}
public string roleName{get; set;}
}
public class Language
{
public int Id{get; set;}
public string name {get; set;}
}As represented above, In my Db, i have primary/foriegn key relationship between (User and Role ) and (User and Language).
I have a registration form which contains various fileds. I am using jquery.ajax to submit the form and pass the values from client to server using JSON object.
Below is my ajax method
function onFinish() {
var Myobj = {
name: $('#<%=txtFirstName.ClientID %>').val(),
email: $('#<%=txtEmail.ClientID %>').val(),
pwd: $('#<%=txtPassword.ClientID %>').val(),
language:
{
Id: $('#<%=ddlLng.ClientID %> option:selected').val(),
name: $('#<%=ddlLng.ClientID %> option:selected').text()
}
};
var myData = JSON.stringify(Myobj);
$.ajax({
url: '<%= ResolveUrl("~/Reg.aspx/Register") %>',
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: '{obj:' + myData + '}',
success: function (response) {
alert(response.d);
},
error: function () {
alert('Error');
//do nothing
},
failure: function () {
alert('Failure');
//do nothing
}
});
}Below is my codebehind
[System.Web.Services.WebMethod]
public static string Register(User obj)
{
try
{
//write logic and return a result
}
catch (Exception)
{
}
return "This is test!!";
}I have run my code and observe few things as below
1. The KEY name in ajax should match with the entity property name (Ex: name, email, pwd). If the KEY name changes, i get a null value in object in codebehind.
2. I have to pass all the values related to an entity while sending the data from ajax. For example "User" entity i have properties like (userid, userRole, isUserActive). Since for a user registration these values are not possible to be passed since its new registration, therefore ajax function expects all the values to be passed and hence when not found gives and error "500 Internal Error"
As we all know the ajax code is visible using developer tool(or by Viewing Page Source). Therefore I am trying to avoid expose of few fields like userId, userRole etc since those fields are only for internal purpose and security related.
Now when this is the scenario, what is the best way that I can accomplish my task?
Please guide on the same.
Thanks,
mds2907