繁体   English   中英

如何使用模型绑定器将数据从跨度的形式+值发布到数据库

[英]how to use model binder to post data from a form + value of a span to data base

美好时光。 我是Asp.net-MVC的新手。

我有一个表单,它从用户那里获取数据,并且此页面中有一个跨度,我想将输入数据和跨度值发布到我的数据库中。

我想使用模型活页夹,但我不知道如何命名与我的模型相同。

这是我的行动:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Id,TourId,FirstName,LastName,Email,Phone,Comment,FrequentTraveler,TravelersCount,Date,ContactTimePreference,Country,Archived")] Request request)
    {
        if (ModelState.IsValid)
        {
            db.Requests.Add(request);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(request);
    } 

span从JavaScript中获得价值

<span class="col-md-5" id="selecteddate"></span>

这是我的表格,但我在这里只写了速记的输入之一。

 <form role="form" method="post" action="/Tour">
  @Html.AntiForgeryToken()

 <div class="form-group">
 <label for="FName">First Name:</label>
 <input type="text" name="FirstName" class="form-control" id="FName" placeholder="Enter FirstName" />


 </div>

</form>

我想将span id =“ selecteddate”的值发布到名为Date列中的数据库中

感谢有人可以告诉我这是怎么可能的,或者您有什么更好的建议吗?

一种解决方案是,将hidden html input添加到<from> 提交时,您可以使用javascript将其值写为跨度的副本。

<form role="form" method="post" action="/Tour" onsubmit="prepare()">
  @Html.AntiForgeryToken()

 <div class="form-group">
 <label for="FName">First Name:</label>
 <input type="text" name="FirstName" class="form-control" id="FName" placeholder="Enter FirstName" />


 </div>
<input type="hidden" name="Date" />
</form>

@section Scripts {
  <script type="text/javascript">
    function prepare() {
        document.getElementsByName("Date")[0].value = document.getElementById("selecteddate").innerHtml;
    }
  </script>
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM