It is bad design to return a partial view that only contains a java script warning in my asp.net MVC

I have the following action method that returns a partial view _error

in case of an exception: -

[AcceptVerbs(HttpVerbs.Post)]
public PartialViewResult Register(string id, int classid) {
    try
    {
        Thread.Sleep(3000);
        User user = r.FindUser(id);
        Users_Classes uc = new Users_Classes();
        uc.AddedDate = DateTime.Now;
        uc.ClassID = classid;
        user.Users_Classes.Add(uc);
        r.Save();
        ViewBag.classid = classid;
        return PartialView("_usersearch2", uc);
    }
    catch (DataException ex)
    { 
        return PartialView("_error");
    }

      

and the following partial view _error

: -

<script type="text/javascript">
    alert('The user might have been already Assinged, Search Again to get the latest users');
</script>

      

The above approach works fine, but does it consider a bad design for returning a partial view to only display a warning? and is there a better way to do this?

+3


source to share


3 answers


The problem is when you are binding your implementation to your UI. The controller suddenly decides how the error message should appear on the client.

What if you want to change it from warning to showing a red border around text input with some description next to it?



The definition of how something should be displayed depends on your view. Your controller should return status codes, and then your judgment should decide what to do.

+3


source


Instead of returning inline js, the js library should have error handling code on your client side. Instead of returning a js hole, return a message.



0


source


In general, I would say yes. But sometimes bad design is what the doctor ordered;)

There's a Controller instance method called there Javascript

that I use to return executable javascript from my controller, in very limited cases where it takes time to do this, the "correct" way is not possible:

[AcceptVerbs(HttpVerbs.Post)]
public PartialViewResult Register(string id, int classid) 
{
    try
    {
        ... stuff
    }
    catch (DataException ex)
    { 
        return Javascript("alert('The user might have been already Assinged, Search Again to get the latest users');");
    }
}

      

The fact that something like this exists gives me comfort that I am not completely breaking the law. If I'm not mistaken, I probably have this.

0


source







All Articles