1

you can say that I'm new in C# MVC and having some problems, been researching a lot, and this is my first topic here.

I'm developing an application that uses data of active directory. I have a table User that import Data from AD. here is my controller create code:

[HttpPost]
    public ActionResult Create(User user)
    {
        if (ModelState.IsValid)
        {
            DirectorySearcher directorySearch = new DirectorySearcher();
            directorySearch.Filter = "(&(objectClass=user)(SAMAccountName=" + user.IdUser + "))";
            SearchResult results = directorySearch.FindOne(); 
             if (results != null)
            {
                if (results.Properties.Contains("mail")) //E-mail
                  {
                      user.E_mail = results.Properties["mail"][0].ToString();
                  }
               else
                  {
                     user.E_mail = string.Empty;
                  }
                if (results.Properties.Contains("c")) //Country
                {
                    user.Country = results.Properties["c"][0].ToString();
                }
                else
                {
                    user.Country = string.Empty;
                }


            }

            db.User.Add(user);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.CompanyCode = new SelectList(db.Company, "CompanyCode", "CompanyName", user.CompanyCode);
        ViewBag.Country = new SelectList(db.Country, "CountryName", "CountryName", user.Country);
        return View(user);
    }`

this is some of my create view code:

    <div class="editor-label">
        @Html.LabelFor(model => model.IdUser)

    </div>

    <div class="editor-field">
        @Html.EditorFor(model => model.IdUser)
        @Html.ValidationMessageFor(model => model.IdUser)      

what I want to do is check in the AD if the user really exists with a function and return the value to the IdUser field but before click the submit, just like a check names.

A solution that I was thinking of, in the view just put a button:

        <p>
    <input type="submit" value="Check" onClick="CheckFunction(model.IdUser)" />
    </p>

and the function is something like:

        public string CheckFunction(string Id)
    {
            DirectorySearcher directorySearch = new DirectorySearcher();
            directorySearch.Filter = "(&(objectClass=user)(SAMAccountName=" + Id + "))";
            SearchResult results = directorySearch.FindOne();
            if (results != null)
            {
                return Id;
            }
            return string.Empty;
    }

but the problem is that I dont know how to set the Id string returned in Function to the model.IdUser field.

Can you guys help me?

2
  • do an ajax jquery call to the controller. see my answer here stackoverflow.com/questions/19359204/…. in the success you can set the result to the id field using $('.IdUser').val(result.id) or whatever you pass back Commented Nov 6, 2013 at 20:40
  • Don't know if I understand, could you explain more detailed? Commented Nov 6, 2013 at 21:49

1 Answer 1

0

you will need to put a class on your button (class="btnSave" for the code below)

$('.btnSave').on('click', function(e){
    $.ajax({
        url: '@(Url.Action("CheckFunction", "Controller"))',
        type: 'post',
        data: {
            //this is the id that is passed.  change @Model.Id to whatever id you want sent
            Id: '@Model.Id'
        },
        success: function (result) {
            if(result.Success){
                //if this is a submit button it will do a post back.  If just a button you can set fields with the returned data or alert, etc
                $('.fieldId').val(result.Id);
            }else{
                //e.preventDefault() will stop the submit button from submitting if failed is returned
                e.preventDefault();
                //do something for failure (alert or something)
            }
        }
    });
});

then change your controller to return json for success or failure. here is an example for that json success and fail response in MVC

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

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.