从View传递一个大HTML字符串控制器 [英] Passing a large HTML string from View to Controller

查看:93
本文介绍了从View传递一个大HTML字符串控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我是一个前端开发人员穿着.NET开发人员的帽子了。笑死,我知道,但是我怎么在这个烂摊子最后是不是这个问题的要点。与免责声明出路,这里发生了什么。

Note: I'm a front-end developer wearing a .Net developer's hat for now. Laughable, I know, but how I ended up in this mess is not the point of this question. With that disclaimer out of the way, here's what is happening.

正如标题所暗示的,我需要通过一个相当长的HTML字符串(如文本的多页)从视图控制器。我花了几天研究各种方式来实现这一目标。 。TBH,是有意义的,而一些没有一些事情。

As the title suggests, I need to pass a quite long HTML string (as in multiple pages of text) from a View to a Controller. I spent the last few days researching various ways to achieve this. TBH, some things made sense while some didn't.

下面里面是我的一段代码查看

Here's my code piece inside View:

var html =
    "<form id='htmlContent' action='" + customUrl + "' method='post'>" +
        "<input type='hidden' name='content' id='contentStr'>" +
    "</form>";

// string literal html gets appended to some element...

$("#htmlContent").submit();



有几件事情,我想在这里指出:

A few things I'd like to point out here:


  • 我使用一个字符串文字在这里构建形式,因为这需要DOM在某个时刻被动态连接到其他元素。

  • 无论我使用一个有效的HTML字符串是出了问题。我已经单独测试它的有效性,一切都看起来很好。

  • 我故意使用jQuery的提交()方法,而不是使用阿贾克斯呼叫

  • I'm using a string literal to construct the form here because this DOM needs to be dynamically attached to other element at some point.
  • Whether I'm using a valid HTML string is out of the question. I've already tested its validity separately and everything looks fine.
  • I'm intentionally using jQuery's submit() method instead of using Ajax call.

控制器:

[HttpPost]
public ActionResult ParseHtml(FormCollection form)
{
    string htmlStr = form["content"].ToString();
    ....
    // some code in between, but the first line appears to be causing an error or
    // the value is not being retrieved.
    ....
    return new EmptyResult();
}



我明白我的MVC框架的范围内工作,我的排序理解它的概念。但是,知道如何用我有限的知识落实是另一回事(尤其是当你继承人谁是长在你的项目了一个糟糕的代码库!)

I understand I'm working within the context of MVC framework and I sort of comprehend the concept of it. But, knowing how to implement it with my very limited knowledge is another thing (especially when you inherited a bad code base from someone who's long gone from your project!)

我倒是愿意认为这是非常简单易做,但我一直在纺纱我的车轮更长的时间比我想。正确的方向的任何指针将非常感激。

I'd like to think this is quite straightforward and easy to do, but I've been spinning my wheels for much longer than I'd like to. Any pointers to the right direction will be much appreciated.

推荐答案

在这个最小可重复的答案,我会告诉你如何获得

In this minimal reproducible answer, I'll show you how to get this working, and you can run with it from here:

Index.cshtml

Index.cshtml

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        var url = '@Url.Action("Create","Stack")';
        var html = $("<form id='htmlContent' action='"+url+"' method='post'><input type='hidden' name='content' id='contentStr' value='oranges'/></form>");
        $(body).append(html);

        $("#htmlContent").submit();
    });
</script>

@{
    ViewBag.Title = "title";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>title</h2>



Controller.cs

Controller.cs

using System.Web.Mvc;

namespace MvcPlayground.Controllers
{
    public class StackController : Controller
    {
        //
        // GET: /Stack/

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

        public ActionResult Create(FormCollection form)
        {
            string htmlStr = form["content"].ToString();

            return View("Index");
        }
    }
}

如果您放置一个断点在返回查看(「指数」); ,你会看到htmlStr是橙子,这是附加的文本框的值

If you place a breakpoint on the return View("Index");, you'll see htmlStr is "oranges", which is the value of the appended textbox.

这篇关于从View传递一个大HTML字符串控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆