2013-03-11 55 views
0

一直在爲此而苦苦掙扎。嘗試了我能想到的一切。使用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(存儲過程)執行完美。希望你可以看到一些明顯的

歡呼

+0

不知道如何用Razor來管理它,但通常如果我想用JavaScript上傳到數據庫,我使用ajax。這是一個PHP實現,但JavaScript是相同的無論如何。它只是將表單值傳遞給上傳的服務器代碼。 http:// stackoverflow.com/questions/15107453 /上傳文件到php-through-ajax' – Ortund 2013-03-11 09:15:13

+0

歡呼ortund。我將在稍後使用ajax。還沒有使用ajax,所以要做到這一點,讓一切都到位,然後擺弄ajax。也許我現在應該只是看一看:P謝謝 – mxadam 2013-03-11 23:26:53

回答

0

,我認爲是錯誤輸入的問題,請檢查您的最後<a>標籤,你在onclick事件類型following.(),看到你的JavaScript函數被調用followTag

如果不解決它,然後擺脫foolowTag功能,您可以指定在窗體本身的動作和控制器,就像這樣:

@using (Html.BeginForm("FollowTag", "YourControllerName", FormMethod.Post)) { 
    ... 
    //Delete this line 
    //<a href="#"class="bttn green large" onclick="return following.();">@T("Follow")</a> 
    //This submit button will do the job 
    <input type='submit' value='@T("Follow")' /> 
} 

應該這樣做。如果您使用的是錨定標籤只是爲了確定樣式,否則您應該使用其他方式,我認爲它更清晰,並且還利用了剃鬚刀的強大功能。

+0

,非常有幫助。希望我可以把這個作爲答案,但沒有得到代碼工作,當它傳遞值時非常奇怪,然後返回頁面,但沒有任何分貝,非常令人沮喪。感謝壽爲此:) – mxadam 2013-03-11 23:24:25

+0

嘿,如果你能幫助這條路線是正確的? 從標籤/樣品調用,但真正標籤/ {tag}是標籤爲約束的路徑 – mxadam 2013-03-11 23:49:23

+0

mxadam 2013-03-11 23:50:29