簡體   English   中英

將大型HTML字符串從View傳遞給Controller

[英]Passing a large HTML string from View to Controller

注意:我現在是戴着.Net開發人員的前端開發人員。 可笑,我知道,但是我最終陷入這個混亂局面並不是這個問題的重點。 有了免責聲明,這就是正在發生的事情。

正如標題所示,我需要將一個相當長的HTML字符串(如在多頁文本中)從View傳遞給Controller。 我花了最后幾天研究各種方法來實現這一目標。 TBH,有些事情是有道理的,有些則沒有。

這是我的代碼片段里面的視圖

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();

我想在此指出一些事項:

  • 我在這里使用字符串文字來構造表單,因為這個DOM需要在某個時刻動態地附加到其他元素。
  • 我是否使用有效的HTML字符串是不可能的。 我已經分別測試了它的有效性,一切看起來都很好。
  • 我故意使用jQuery的submit()方法而不是使用Ajax調用。

控制器:

[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框架的上下文中工作,我有點理解它的概念。 但是,知道如何用我非常有限的知識來實現​​它是另一回事(特別是當你從一個早已離開你的項目的人那里繼承了一個糟糕的代碼庫時!)

我想這很簡單易行,但我的輪子旋轉時間比我想要的長得多。 任何指向正確方向的指針都將非常感激。

在這個可重復性最小的答案中,我將向您展示如何使其工作,您可以從這里運行它:

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

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");
        }
    }
}

如果在return View("Index");上放置斷點return View("Index"); ,你會看到htmlStr是“oranges”,這是附加文本框的值。

不回答問題的主要部分,因為我不確定所有細節,抱歉。 我並不是說你在任何方面做錯都是“錯誤的”因為我不確定外圍可能會讓它看起來像是必要的,但你可能會創造一種你將會頭疼的局面在后端解析和什么不解析。 由於我不確定這些要求所以很有可能這很多都是錯的,所以我道歉。

"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."

您想要在另一個組件中重新渲染相同的元素嗎? 這聽起來像是在尋找局部視圖。 有了它,您可以在View1中渲染它並在View2中使用相同的數據。 如果你正在嘗試動態創建元素,我想你正在使用某種類型的模式(即我正在研究一個項目,我在那里迭代一個集合並做

<input type='text' id='attribute_@(item.Id)' name='attribute_@(item.Id)' />

然后遍歷FormCollections AllKeys元素來獲取它們(解析出attribute_以及你喜歡什么:

foreach(var key in form.AllKeys)
{
  var id = Convert.ToInt32(key.Replace("attribute_", "")); // may want to TryParse
  var item = Item.GetItemById(id);
  item.Value = form[key];
  item.Update();
}

在視圖的某個地方,你使用javascript來設置document.getElementById(“contentStr”)(不確定是否有意,但你的id和名稱是不同的。本身不是壞事,但可能導致oops)? 然后再次,不確定var html =在這里使用了什么。 Razor認為你可以只做[form] ... [/ form]的常規html。 你是否在提交行動之前做了類似的事情?

$('#content').val(html);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM