0

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

4
  • I'm not sure I totally understand your question - can you elaborate on it? Why do you have a problem with your Edit view? And why is it a problem that the same remote validation is applied for both Create and Edit? Commented Sep 2, 2012 at 15:50
  • @Lasse Christiansen - sw_lasse: See Remote validation is applied on Model attribute Title So, whenever I operate on model, It Checks for this action. Commented Sep 2, 2012 at 15:52
  • @Lasse Christiansen - sw_lasse See, In Create form It checks for Duplicate title (returs true / false) this is okay. But in Edit for if I update with same Title, again it checks for Duplicate title with "Title to be Edited" and it says Title already taken. This is the Problem Commented Sep 2, 2012 at 15:58
  • I see, thanks - I have now posted a possible solution :) Commented Sep 2, 2012 at 16:15

4 Answers 4

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

What if you do not want to use it as a hidden field, but as an editable field ?
1

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);
    }

Comments

0

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

Comments

0

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);  
}    
}

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.