Coder Social home page Coder Social logo

prg's Introduction

AdamTibi.Web.Prg NuGet package and sample code

This is an ASP.NET MVC Core NuGet Package to support the PRG pattern. You can see a description about the PRG pattern here: https://andrewlock.net/post-redirect-get-using-tempdata-in-asp-net-core/ and some of the code used in this package is adopted from this post by Andrew Lock.

The package includes 2 attributes, PreserveModelStateAttribute and RestoreModelStateAttribute that you should use on your action methods when you intend to redirect the PRG style. These two classes will provide a clean way of saving and restoring your ModelState.

Using the NuGet package

[HttpGet]
[RestoreModelState(nameof(ContactUsSubmit))]  // Add here
public IActionResult Index()
{
	ContactUsViewModel contactUs = new ContactUsViewModel
	{
		CurrentTime = DateTime.Now,
		ResponseTime = 2 // Assume this is a dynamic value that we are getting from somewhere
	};
	return View(contactUs);
}

[HttpPost]
[PreserveModelState(nameof(ContactUsSubmit))] // Add here
public IActionResult ContactUsSubmit(ContactUsViewModel contactUs)
{
	if (!ModelState.IsValid)
	{
		// Show the form with error messages
		// This is the right way, PRG, redirecting, rather than returning the View
		return Redirect("/");
	}

	// process email and message from the contactUs object
	// ...

	// Show the user a thank you page.
	// This is the right way, PRG, redirecting, rather than returning the View
	return Redirect("thank-you");
}

[HttpGet]
[ActionName("thank-you")]
public IActionResult ContactUsThankYou()
{
	return View("ContactUsThankYou");
}

prg's People

Contributors

adamtibi avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.