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?