[英]how to use model binder to post data from a form + value of a span to data base
Good time. 美好时光。 I'm new in Asp.net-MVC .
我是Asp.net-MVC的新手。
I have a form which it gets data from user and there is a span in this page which I want to post data of inputs and value of span to my data base. 我有一个表单,它从用户那里获取数据,并且此页面中有一个跨度,我想将输入数据和跨度值发布到我的数据库中。
I wanted to use model binder but I don't know how to name spans same as my model. 我想使用模型活页夹,但我不知道如何命名与我的模型相同。
here is my action : 这是我的行动:
[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 is getting value from JavaScript span从JavaScript中获得价值
<span class="col-md-5" id="selecteddate"></span>
and here is my form but I write just one of the inputs here for Shorthand. 这是我的表格,但我在这里只写了速记的输入之一。 :
:
<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>
I want to post value of span
id="selecteddate" to data base in column named Date
我想将
span
id =“ selecteddate”的值发布到名为Date
列中的数据库中
appreciate if any one could tell me how it could be possible or if you have any better way as suggestion? 感谢有人可以告诉我这是怎么可能的,或者您有什么更好的建议吗?
One solution for this is, add a hidden html input
to your <from>
. 一种解决方案是,将
hidden html input
添加到<from>
。 When you do your submit, you write its value as a copy from your span using javascript. 提交时,您可以使用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.