簡體   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