从视图中发布整个div内容

时间:2014-08-05 10:53:23

标签: html asp.net-mvc razor

下面的

<div>位于.cshtml文件中。它将被保存到控制器中的数据库中。然后它将显示为html。请参阅span元素的contenteditable属性。如果它显示&#34;点击更改&#34;,用户将点击并放置,然后发布。 <div>元素也不允许具有name属性。如何将整个内容按原样发送到数据库?

<form id="newadform" action="/ad/new" method="post">
   <div>
       <font color="#000000" face="Times New Roman" size="3">
       <p align="center" style="margin: 0cm 0cm 8pt;">
       <img width="730" height="380" src="https://abc.blob.core.windows.net/help/bbca-100.png"></p></font>
       <p><b><span>&nbsp;</span></b></p>
       <p><span contenteditable="true" onkeypress="return(this.innerText.length <= 80)" >CLICK TO CHANGE</span></p>
       <font color="#000000" face="Times New Roman" size="3"></font>
       <p style="background: white; margin: 0cm 0cm 0pt; line-height: normal; vertical-align: baseline; mso-outline-level: 2;">
       <span lang="TR" style='color: rgb(217, 72, 69); font-family: "Arial","sans-serif"; font-size: 16.5pt; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: TR;'>&nbsp;</span>
       </p>
       <p><span lang="TR" >Definition</span></p>
       <p><span lang="TR" style='color: rgb(217, 72, 69); font-family: "Arial","sans-serif"; font-size: 16.5pt; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: TR;'>&nbsp;</span></p>
   </div>
</form>

编辑:

 public ActionResult New(string id, string pid, AdDetail model)
 {
     Operation operation = new Operation(User.Token, 2);
     AdOperation omodel = GetModel(new AdOperation(), model);
     UpdateAd ohelper = operation.SaveAd(omodel);
     return Redirect("/ad/detail/" + ohelper.Model.AdId);
 }

AdDetail类(型号):

 public class AdDetail: BaseVRqH
 {
    public string txt1 { get; set; }
    public string txt2 { get; set; }
    public string AdCode{ get; set; }
 }

2 个答案:

答案 0 :(得分:1)

$("form").submit(function{
   $.ajax({
        url: "/ControllerName/SaveHtml",
        type: 'POST',
        dataType: 'json',
        data: { divhtml:$("#div1").html() },
        success: function (data) {}
        });
});

   [HttpPost] 
   [ValidateInput(false)]  <----------this is necessary to disable inbuilt MVC Security(in order to save html tags)
   public JsonResult SaveHtml(string divhtml)
    {
        // Save html to database
        return Json(new{ result="true" }, JsonRequestBehavior.AllowGet);
    }

只需提供div个ID div1

即可

答案 1 :(得分:0)

看看下面的示例代码:

在您的模型中再次声明属性,例如视图中的“blow”和“Hidden”字段。

在您的视图中,只需添加Jquery Submit event即可。何时click on form submit。只需将html文本分配给该属性即可。喜欢下面。

<强>型号:

public class MyViewModel
{
    [AllowHtml]
    public string RequestText { get; set; }  // You will get this html in this property
}

<强>控制器:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            RequestText = "<strong>Hello World</strong>";
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        // Save operations Goes Here
        return View(model);
    }
}

查看:

@model MyViewModel
@using (Html.BeginForm())
{
    @Html.TextAreaFor(x => x.RequestText) // Remove or Comment this line

    <input type="hidden" name="RequestText" id="RequestText">

    <button type="submit">OK</button>
}

<script>
 $( document ).ready(function() {
  //if you want to bind the html to this then assign the requesttext property value here... Like below...

  $('#divHtml').html('@model.RequestText'); // properly you can assign your model here

  $('input[type="submit"]').click(function () {
     $('RequestText').val($('#divHtml').html());
     return true;
  });
 });

</script>

来自Stackoverflow - Darin Dimitrow

的参考资料

我刚刚Copy Cat work Here解决了你的问题。 :)