Example on View to Controller (HttpPost) & warn on duplicate entry & delete duplicates in MVC

This is a quick response post to the blog reader ‘V Patel’ who asked this question yesterday. Here is the question:


His main question was:

i) passing data from view to controller and
ii) on button click display the thing from textbox to label.

To answer these quick questions I build a sample web app where the user will register his mobile number to get SMS alerts.


Even when the user tries to post a blank form, the client side jQuery validation prevents it and displays a suitable message, as in:


In addition to all these, you can display the message to the user that you are trying to duplicate the entry or even you can delete the previous entry and take a new entry when duplication occurs intelligently without information to the user.


Now, I would like to share all the code here.

Model Class


Index View


Controller


If you wish to delete the previous entry and use the new entry when duplication is detected by the system, use the following HttpPost:


It is very simple so far, like web forms. So, in this post you have seen how I passed the information entered by the user from the View to the Controller to check duplication and if a duplicate is found again then the error message is passed back to View.

If you just want to display information entered in the textbox to the label (as in the question), then this is quicker:


Hope this helps. Thanks.

Comments

  1. Thanks buddy, that was very helpful.

    Good explanation

    Here is the point... Even for a normal scenario like taking just one/two input i will need to create a model class, so basically their is no easy way to deal with this situation on the other side CRUD operations and basic plumbing of code is very easy with MVC. +/- of MVC

    Keep doing all the good work.

    ReplyDelete
    Replies
    1. Thanks. Well, MVC is actually a architecture so you still can use web forms there and do things like in web forms if you are happy, model is not necessary.

      Delete
  2. Thanks buddy, that was very helpful.
    and it works almost Properly for me. but the message we are showing is not printed as i have make my control as named student and having all default view(index, detail, edit, delete, create) and i have applied code for create. it is properly checking it and returning back without insert but not Printing message.

    here is code

    [HttpPost]
    public ActionResult Create(Student student)
    {
    var mobile = student.Mobile;
    var user = (from x in db.Students where x.Mobile == mobile select x).ToList();
    if (ModelState.IsValid)
    {
    if (user.Count > 0)
    {
    ViewBag.Duplicate = " Mobile No. " + mobile + " is alwready exists.";
    }
    else
    {
    db.Students.Add(student);
    db.SaveChanges();
    ViewBag.SuccessMesg = " Mobile No. Saved.";
    ViewBag.Notice = " you will soon get a verificaion message on Mobile No.";
    }
    return RedirectToAction("Index");
    }

    return View(student);
    }

    Thanking you

    ReplyDelete
  3. i have done it with session. but as we are not able to use viewState in MVC can you please send me some of example like deserialization or else we can use so for particular page.

    ReplyDelete
    Replies
    1. thanks for comment. you are not getting ViewBag.Duplicate message on view page..right? Use break-point in debut mode to check whether controller returning value for ViewBag.Duplicate or not? You can also try TempData.Duplicate.

      Delete

Post a Comment

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System