Unknown Coder
Unknown Coder

Reputation: 1740

How to handle Remote Attributes on Model Edit? ASP.Net MVC3

I'm using Remote Attribute in my Model to check duplicate Page Titles as follows

public class Page
{
  [Remote("CheckDuplicate", "Page", ErrorMessage = "Title already taken")]
  public string Title { get; set; }
}

And In controller, I'm returning JsonResult data based on "Check" result as follows:

public JsonResult CheckDuplicate(string Title)
{
   var result = db.Pages.Where(a => a.Title == Title).Count() == 0;
   return Json(result, JsonRequestBehavior.AllowGet);
}

This is working fine in Create action, But problem is, It's restricting me to Edit the Existing page, Since It's checking the same query.

How to solve this Problem? Please Suggest me something

Upvotes: 0

Views: 3036

Answers (4)

Hemal
Hemal

Reputation: 3760

I think it should be this way

You must have a hidden field in Edit page as InitialUsername and a Remote attribute on User model with AdditionalFields containing InitialUsername

Controller

    [HttpPost]
    public JsonResult doesUserNameExist(string UserName,string InitialUsername)
    {
        User user=null;
        //Check if user already exists
        if (UserName.Equals(InitialUsername)==false)
        {
         user = db.Users.Where(u => u.USERNAME == UserName).FirstOrDefault();
        }
        //

        return Json(user == null);
    }

Upvotes: 1

Sandip
Sandip

Reputation: 35

i was solve using following steps: In my view, I put @Html.Hidden("InitialUserName", Model.UserName);

On Model put : [Remote("IsUserNameUsed", "User", AdditionalFields="InitialUserName")]

In Controller actionmethod write actionmethod as followiing :

    public JsonResult IsUserNameUsed(string UserName, string InitialUserName){
    bool isExist = true;
    if(UserName  != string.empty && InitialUserName == "undefined"){
    var isexist= db.model.where( x=>x.UserName == UserName).single();
    if(isexist != string.empty){
    bool isExist = false;
    }

    if(UserName  != string.empty && InitialUserName != "undefined"){
    var isexist= db.model.where( x=>x.UserName == UserName && x.UserName != InitialUserName ).single();
    if(isexist != string.empty){
    bool isExist = false;
    }    
return Json(isExist , JsonRequestBehavior.AllowGet);  
}    
}

Upvotes: 0

Sandip
Sandip

Reputation: 35

Just Add the Html attribute where you want to disable the remote validation like

 @Html.EditorFor(model => model.CODE, 
                    new { 
                          htmlAttributes = new { 
                                                 @readOnly = true, 
                                                 @data_Val = false } })

and also see the HiddenField value if u stored It will be work

Upvotes: 0

Lasse Christiansen
Lasse Christiansen

Reputation: 10325

Your question looks similar to this: ASP.NET MVC 3 Remote validation to allow original value

I think the trick is to use the AdditionalFields argument to your remote validation attribute in your model and combine that with a hidden field in your view - like suggested in the above StackOverflow post. Then you can send in the "initial" value along with the new value to your remote validation method and use both arguments to do your uniqueness check.

Another example of how to fix this can be found here: https://stackoverflow.com/a/4756796/700926

The documentation for AdditionalFields can be found here: http://msdn.microsoft.com/en-us/library/system.web.mvc.remoteattribute.additionalfields(v=vs.98).aspx

Upvotes: 5

Related Questions