Friday, 12 April 2019

Overrite the Action Link in MVC



In my topic where I reused the view inside another main view, the problem occurred on an Action link, because we added the list views of contact and opportunity in the model view of Account, therefore when you see the edit, detail, delete or even Create hyperlink, you would see that it is pointing to the Account view itself which is not the right thing, it should redirect to the contact and opportunity views, like below screenshot



Means, you are inside the account detail page and if you mouse over the edit, detail, delete or even Create hyperlink then you would see the Account/Edit/2, see above red highlighted area. Now, we should also fix this by overwriting the Action Link, Link should be “/Contacts/Edit/2”. For this fix, created a class of URLs and added a static method like below one

public class URLs
{
 public static string ContactDetail(int ContactId,string Type)
 {
        string URL = string.Empty;
        if (Type == "Edit")
              URL = "/Contacts/Edit/" + ContactId.ToString();
        else if (Type == "Details")
              URL = "/Contacts/Details/" + ContactId.ToString();
        else if (Type == "Delete")
              URL = "/Contacts/Delete/" + ContactId.ToString();

        return URL;
 }
}

Above code shows how we overwrite the Edit, Detail and Delete URL, now this method called inside the list views of contact, a similar method can be made for an opportunity to replace the Action Link



Above screenshot is the combination of 2 pictures, the right side is the top view code which shows how we use the URLs class and overwrite the CREATE ACTION LINK and left side of the picture shows how we overwrite the detail, edit and delete action link.

Call main view inside the view like partial view in MVC



In MVC, normally users create a partial view and reuse the partial view in any of the main view. The developer generally creates a partial view using point and click option.




  
And when we generate this partial view, there is nothing in the view which identifies that this is a partial view like below



I was new in MVC pattern and worked on asp.net web forms, therefore I asked my developer fellows why we mark to check that it is partial view and use this view as a partial view, why we can’t reuse the normal view as a partial view, I got no answer. Therefore I tried something which I wanted to do 😊. My scenario is that I have an Account model which is the parent table, Contact and Opportunity are the children of Account. I wanted to show the list views of Contact and Opportunity in the Account Detail view, already made a complete set of views of Contact & Opportunity which are behaving independently. Let’s take the example of Contact, Contact list view is driven from the stored procedure and looks like below



 To call this contact list view inside the detail view of account, use the below code in Account Detail view



Above snapshot shows the complete code which is rendering the list views of contact & opportunity as a partial view inside the detail view of Account view. Firstly I called the same stored procedures and pass the account id which actually filters out the contacts and opportunity based on the account id then pass the generic list of contact & opportunity to the list view of contact & opportunity. This code makes our life easy because we only changing the code only at this level, no need to update the list views of contact & opportunity. The actual detail view of the Account now looks like below screenshot





We have learned how we reuse the main views inside any views, no need to create separate partial views, we can create partial views based on requirements