Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Form post最佳实践_C#_Asp.net Mvc - Fatal编程技术网

C# Form post最佳实践

C# Form post最佳实践,c#,asp.net-mvc,C#,Asp.net Mvc,我正在编写我的第一个ASP.NETMVC4应用程序,我正在努力弄清楚我所做的是不是一个好的方法,是一种过火的方法,还是纯粹的愚蠢 我知道有一百万种方法可以做事情,但只是寻找一些最佳实践指导 基本上,我的索引视图呈现一个简单的表单,在POST上我创建了一个PDF,将其存储在会话和ViewBag中,并将其传递给确认操作。所有确认操作都用于显示一个视图(Confirm.cshtml) 有人能带我到这里吗 控制器 public class HomeController : Controller {

我正在编写我的第一个ASP.NETMVC4应用程序,我正在努力弄清楚我所做的是不是一个好的方法,是一种过火的方法,还是纯粹的愚蠢

我知道有一百万种方法可以做事情,但只是寻找一些最佳实践指导

基本上,我的索引视图呈现一个简单的表单,在POST上我创建了一个PDF,将其存储在会话和ViewBag中,并将其传递给确认操作。所有确认操作都用于显示一个视图(Confirm.cshtml)

有人能带我到这里吗

控制器

public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(FormCollection formCollection)
    {

        // Create PDF
        var doc = new Document();
        MemoryStream memoryStream = new MemoryStream();

        PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);

        doc.Open();
        doc.Add(new Paragraph("First Paragraph"));
        doc.Add(new Paragraph("Second Paragraph"));
        doc.Close();

        byte[] docData = memoryStream.GetBuffer(); // get the generated PDF as raw data

        // create id and store data in Session
        var id = Guid.NewGuid().ToString();
        Session[id] = docData;

        // store the id in ViewBag
        ViewBag.id = id;

        return View("Confirm");
    }

    // Handles the /Home/Confirm view
    public ActionResult Confirm()
    {
        return View();
    }

    public ActionResult Download(string id)
    {
        var docData = (byte[]) Session[id];

        if (docData == null) {
            return HttpNotFound();
        }

        Session[id] = null;

        return File(docData, "application/pdf", "test.pdf");
    }

}
查看

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Confirm</title>
</head>
<body>
    <div>
        <a href="/Home/Download?id=@ViewBag.id">Download PDF</a>
    </div>
</body>
</html>
@{
布局=空;
}
证实

从MVC的角度来看:

使用ViewModels是一个很好的实践,请看本教程

一些优势:

视图模型将视图与模型类隔离,并允许模型独立于视图进行演化

这将在视图模板中启用类型安全、编译时检查和编辑器智能感知

安全性方面,如果您直接将对象从ORM公开给视图,则可以使用视图模型操纵IsAdmin之类的属性,您可以对此进行限制

我会在行动后的索引中更改的内容

return View("Confirm");
致:

因为:

Post/Redirect/Get(PRG)是一种web开发设计模式,可防止重复表单提交


您想查看代码还是想做什么?使用ViewModels的另一个很好的理由是,您可以重定向模型并将其作为参数传递,例如return RedirectToAction(“确认”,model);与ViewBag类似的另一种选择是TempData[“key”],它将一直保留到http请求,并将在此之后自动删除。语法类似于ViewData。更多信息:我还没有试过,但听起来正是我需要的,我会接受你的答案。谢谢
return RedirectToAction("Confirm");