Handling status message upon submitting asp mvc form [closed] - asp.net-mvc

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have been using asp MVC model state error when handling errors.
currently, I was using temp data as you see below:
TempData["message"] = "Successfully Added New Data.";
and rendered in all my views with forms.
I just want to ask any best practice that can be used when it comes to handling success message/status.
TIA!

If you are redirecting to another view you can use TempData collection. If you are staying on the same view, I would return current view with a flag in the model and then render it base on the value or just use ViewBag.

You should include the error or success message in the model you sent to the view.
Something like that:
public async Task<IActionResult> CityDetails()
{
MyModel MyModel = new MyModel();
MyModel.Message = "Successfully Added New Data.";
return View(MyModel);
}

Related

ASP.NET MVC controllers best practice for method naming [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I know next to nothing about MVC so my question might look a bit basic but here it is anyway : is it a good practice to have the same name for the method returning the view and the one saving the data ?
I see a lot of examples like this one where the overloaded "Create" does both jobs. Here's a snippet:
//
// GET: /Customer/Create>
public ActionResult Create()
{
return View();
}
//
// POST: /Customer/Create>
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here>
return RedirectToAction("Index");
}
catch
{
return View();
}
}
I personnaly find it confusing when overloads do completely different things depending on which I decide to choose. So what do you think ? Is it really the "best practice" to roll that way?
Yes, the GET and the POST in this case usually share the same name, because they relate to the same user action.
See Action Naming Convention for more guidance on Action naming.
I personally find it confusing when overloads do completely different things depending on which I decide to choose.
Do they really? They are both concerning the same entity (a Customer). What you can do is name methods whatever you want and then add the [ActionName] attribute and that will be the name of the action. But yes. it's an MVC convention to name them the same way and then pick one or the other depending on whether you're using GET or POST.
Yes if it is the POST method for that view then you should stick to the same name. The only time you should not follow this convention would be if you have multiple different POST methods for the same view for some reason or if you have a POST method which is used by multiple different views.
To provide a standard example:
You have a Create view which is used to create users. There is a HTTPGET Create() action and a HTTPPOST Create() action for posting the user details to the server. In this case you should use the same name.
You have a Dashboard view which displays some data from the database. You have some javascript which uses ajax to retrieve data from the server. In this case you may consider using a different name. E.g. GetData()

How do I the site logo depending on the URL the request was made from? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I created a product registration page using ASP.Net MVC. I need to implement the same product registration page for subsidiaries (maybe 3 or 4) with a few minor changes to the way the site looks. For example, the logo will be different and some text at the top of the page. What is the best way to use the same codebase?
The best option I could come up with is passing HttpContext.Current.Request.URL to the view and using java script to update it.
However, I know routing can be an option too.
If you will be keeping the same .cshtml view for all registration pages then i think creating a partial view for logo generation would probably help you out.
Add another variable to your view model.. maybe call it subsidiary?
public int Subsidiary { get; set; }
then create a partial view called something like _LogoPartial.cshtml and in there do a if statement on the Subsidiary variable and return a different logo based on it
#model int
#if (Model == 1)
{
<img src="something" />
}
else if ...
then in your main view call it with
Html.RenderPartial("~/Views/Shared/_LogoPartial", Model.Subsidiary);

ASP.NET MVC Last visited data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How to show Last Ten Pages Visited Cookie with asp.net mvc?
To store the url for each accessed page, you can use the Application_BeginRequest method of the global.asax, which will be called whatever the page accessed.
Example :
protected void Application_BeginRequest(object sender, EventArgs e)
{
string currentUrl = this.Context.Request.Url.AbsolutePath;
// Store the currentUrl to your database, for example
}

why returning to other view from action is bad? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I m working in MVC and some one recently has reviewed my code. On some places in my controller actions I was returning to other views, for example in below code in Index action I m returning to List view:
public virtual ActionResult Index(Student student)
{
// Logic
return View("List", student);
}
Why it is not a good practice ? I did not get any idea why this is wrong. Kindly guide me so I can correct this.
There is nothing wrong with returning a different view to the default action view. The only thing I can think that might be unsettling to some is it does not following the MVC paradigm of convention over configuration.
This may make it difficult to follow someones code if they have lots of actions with names that are different to their corresponding view names i.e:
public ActionResult Index(int id)
{
//logic
return View("StupidName");
}
Convention would have you think the index action would return the Index view, although it doesn't and now the programmer has to go look for the StupidName view. This may seem harmless for one action but if someone has written multiple actions that return differently named views it can be difficult to follow the flow of the project.
Alternatively you could ask the code reviewer why he thinks it is a bad practice. Communication is key in a team.
Personally I see nothing wrong with this as long as the logic for compiling the input for the view isn't duplicated.
If you have to copy a lot of code to ensure that the model for the view is correct, and this isn't really the responsibility of your action, you'd rather redirect to the action instead of calling the view directly.
If this is not the case, or there is a very specific situation, I don't see why this is a problem.
Consider these examples:
You have a error dialog view. If the model just consists of a single property defining the message, it is no problem to call the view directly;
If you have the same dialog, but in order to create the model you have to fetch some general information from the database, have some custom logic, etc., then it is probably best to call the (or create an) action doing this all for you. Also, the number of calls to that view may matter in your decision.
Try this :
public virtual ActionResult Index(Student student)
{
// Logic
return RedirectToAction("List");
}

How to Add action link in view using razor [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a required field, string attribute { get; set } in a class and want to set its value in Razor. Is something like the following possible?
#model.attribute = "whatever'
Close. #model is how you declare the model for your view. Your view should have a Model property, so you can do this:
#{
Model.attribute = "whatever";
}

Resources