开发者

How to return a message from my repository class to my controller and then to my view in asp.net-mvc?

开发者 https://www.devze.com 2022-12-30 18:26 出处:网络
I use this for checking an existing emailId in my table and inserting it...It works fine how to show message to user when he tries to register with an existing mailId....

I use this for checking an existing emailId in my table and inserting it...It works fine how to show message to user when he tries to register with an existing mailId....

if (!taxidb.Registrations.Where(u => u.EmailId == reg.EmailId).Any())
{
     taxidb.Registrations.InsertOnSubmit(reg);
     taxidb.SubmitChanges();
}

and my cont开发者_开发百科roller has this,

 RegistrationBO reg = new RegistrationBO();
 reg.UserName = collection["UserName"];
 reg.OrgName = collection["OrgName"];
 reg.Address = collection["Address"];
 reg.EmailId = collection["EmailId"];
 reg.Password = collection["Password"];
 reg.CreatedDate = System.DateTime.Now;
 reg.IsDeleted = Convert.ToByte(0);
 regrep.registerUser(reg);

Any sugesstion how to show "EmailID" already exists to the user with asp.net mvc...


Make the registerUser repository method return a boolean value indicating whether it has updated the database so that the controller action becomes:

if (!regrep.registerUser(reg))
{
    ViewData["message"] = string.Format("{0} already exists", reg.EmailId);
}

and in your view show the message:

<div><%= Html.Encode(ViewData["message"]) %></div>

If you are using strongly typed view which is recommended then you may add a boolean property to your view model which will indicate whether the database update took place:

model.EmailId = reg.EmailId;
model.IsEmailExists = !regrep.registerUser(reg);
return View(model);

and in the view test the model value:

<% if (Model.IsEmailExists) { %>
    <div><%= Html.Encode(Model.EmailId) %> already exists</div>
<% } %>
0

精彩评论

暂无评论...
验证码 换一张
取 消