[英]Bind Data Of a span to database with ASP MVC,Uses Model Binder
[英]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.