一直在爲此而苦苦掙扎。嘗試了我能想到的一切。使用JavaScript將數據傳遞給DB IM,正常工作與另一個頁面上整數,但現在用繩子它不會工作:■asp.net javascript to db
@using (Html.BeginForm(null, null, FormMethod.Post, new{@id="manageForm"}))
{
@Html.AntiForgeryToken()
<span class="actions">
@T(User.Id.ToString()) @T(" ") @T(ViewData["Tag"].ToString())
<input type="hidden" name="tag" value="fr" />
<input type="hidden" name="id" value="3" />
<a href="#"class="bttn green large" onclick="return following.();">@T("Follow")</a>
</span>
}
的Javascript
<script type="text/javascript">
function followTag() {
$('#manageForm').attr('action', '@(Url.Action("FollowTag"))').submit();
return false;
}
</script>
控制器
[RequireAuthorization]
[HttpPost]
public ActionResult FollowTag(int id, string tag)
{
_service.FollowTag(id, tag);
return RedirectToAction("TagPage","Detail", new
{
});
}
數據訪問
public void FollowTag(int id, string tag)
{
DbCommand comm = GetCommand("SPTagFollow");
//user id
comm.AddParameter<int>(this.Factory, "id", id);
//id to follow
comm.AddParameter<string>(this.Factory, "tag", tag);
comm.SafeExecuteNonQuery();
}
路由設置正常,並且sql(存儲過程)執行完美。希望你可以看到一些明顯的
歡呼
不知道如何用Razor來管理它,但通常如果我想用JavaScript上傳到數據庫,我使用ajax。這是一個PHP實現,但JavaScript是相同的無論如何。它只是將表單值傳遞給上傳的服務器代碼。 http:// stackoverflow.com/questions/15107453 /上傳文件到php-through-ajax' – Ortund 2013-03-11 09:15:13
歡呼ortund。我將在稍後使用ajax。還沒有使用ajax,所以要做到這一點,讓一切都到位,然後擺弄ajax。也許我現在應該只是看一看:P謝謝 – mxadam 2013-03-11 23:26:53