2009-12-08 53 views
0

我想在我的MVC應用程序的數據庫中插入值,但我通過javascript獲取值。請告訴我該怎麼做,是否有任何方式通過它我們可以在mvc應用程序中處理javascript。請提供一些有用的資源。在mvc中通過javascript在數據庫sql server 2008中插入值/

我的JavaScript代碼是關於通過用戶創建動態字段的動態字段創建。我從用戶那裏獲得值,請告訴我該怎麼做。

的JavaScript代碼如下:

<script language="javascript" type ="text/javascript" > 

function addRow(tableID) { 

    var elem = document.getElementById("linkid"); 
    if (typeof elem.onclick == "function") { 
     elem.onclick.apply(elem); 
    } 

    var table = document.getElementById(tableID); 

    var rowCount = table.rows.length; 
    if (rowCount < 25) { 
     var row = table.insertRow(rowCount); 

     var cell1 = row.insertCell(0); 
     var element1 = document.createElement("input"); 
     element1.type = "checkbox"; 
     cell1.appendChild(element1); 


     var cell2 = row.insertCell(1); 
     var element2 = document.createElement("input"); 
     element2.type = "text"; 
     cell2.appendChild(element2); 


     var cell3 = row.insertCell(2); 
     var element3 = document.createElement("input"); 
     element3.type = "text"; 
     cell3.appendChild(element3); 

     var cell4 = row.insertCell(3); 
     var element4 = document.createElement("a"); 
     element4.setAttribute("href", "#"); 
     var anchortext = document.createTextNode('Edit'); 
     element4.appendChild(anchortext); 
     // cell4.appendChild(element4); 



    } 
    else { 
     alert("you can add only upto 25 fields"); 
    } 

} 
function deleteRow(tableID) { 
    try { 
     debugger; 
     var table = document.getElementById(tableID); 
     var rowCount = table.rows.length; 1 
     for (var i = 0; i < rowCount; i++) { 
      var row = table.rows[i]; 
      var chkbox = row.cells[0].childNodes[0]; 
      if (null != chkbox && true == chkbox.checked) { 
       table.deleteRow(i); 
       rowCount--; 
       i--; 
      } 

     } 
    } catch (e) { 
     alert(e); 
    } 
} 

function hideVisible(divid, divid1) { 
    try { 

     if (divid == "Advertiser") 
     { 
      if (document.getElementById(divid).style.display == "block") { 
       document.getElementById(divid).style.display == "none" 
       document.getElementById(divid).style.visibility = "hidden"; 
       if (document.getElementById(divid1).style.display == "none") { 
        document.getElementById(divid1).style.display = "block" 
        document.getElementById(divid1).style.visibility = "visible"; 
        var text = document.getElementById("advertiser_Text"); 
        text.setAttribute("class", "newtext1"); 
        text.value = "Advertiser"; 

       } 


      } 
     } 


     if (divid == "Brand") 
     { 
      if (document.getElementById(divid).style.display == "block") 
      { 
       document.getElementById(divid).style.display == "none" 
       document.getElementById(divid).style.visibility = "hidden"; 
       if (document.getElementById(divid1).style.display == "none") 
       { 
        document.getElementById(divid1).style.display = "block" 
        document.getElementById(divid1).style.visibility = "visible"; 
        var text = document.getElementById("brand_Text"); 
        text.setAttribute("class", "newtext1"); 
        text.value = "Brand"; 

       } 


      } 
     } 




     if (divid == "Target") 
     { 
      if (document.getElementById(divid).style.display == "block") 
      { 
       document.getElementById(divid).style.display == "none" 
       document.getElementById(divid).style.visibility = "hidden"; 
       if (document.getElementById(divid1).style.display == "none") 
       { 
        document.getElementById(divid1).style.display = "block" 
        document.getElementById(divid1).style.visibility = "visible"; 
        var text = document.getElementById("Target_Text"); 
        text.setAttribute("class", "newtext1"); 
        text.value = "Target"; 

       } 


      } 
     } 




     if (divid == "Timing") { 
      if (document.getElementById(divid).style.display == "block") { 
       document.getElementById(divid).style.display == "none" 
       document.getElementById(divid).style.visibility = "hidden"; 
       if (document.getElementById(divid1).style.display == "none") { 
        document.getElementById(divid1).style.display = "block" 
        document.getElementById(divid1).style.visibility = "visible"; 
        var text = document.getElementById("Timing_Text"); 
        text.setAttribute("class", "newtext1"); 
        text.value = "Timing"; 

       } 


      } 
     } 


     if (divid == "Program") { 
      if (document.getElementById(divid).style.display == "block") { 
       document.getElementById(divid).style.display == "none" 
       document.getElementById(divid).style.visibility = "hidden"; 
       if (document.getElementById(divid1).style.display == "none") { 
        document.getElementById(divid1).style.display = "block" 
        document.getElementById(divid1).style.visibility = "visible"; 
        var text = document.getElementById("Program_Text"); 
        text.setAttribute("class", "newtext1"); 
        text.value = "Program Scope"; 

       } 


      } 
     } 



     if (divid == "Media") { 
      if (document.getElementById(divid).style.display == "block") { 
       document.getElementById(divid).style.display == "none" 
       document.getElementById(divid).style.visibility = "hidden"; 
       if (document.getElementById(divid1).style.display == "none") { 
        document.getElementById(divid1).style.display = "block" 
        document.getElementById(divid1).style.visibility = "visible"; 
        var text = document.getElementById("Media_Text"); 
        text.setAttribute("class", "newtext1"); 
        text.value = "Media Buy"; 

       } 


      } 
     } 

     if (divid == "Distribution") { 
      if (document.getElementById(divid).style.display == "block") { 
       document.getElementById(divid).style.display == "none" 
       document.getElementById(divid).style.visibility = "hidden"; 
       if (document.getElementById(divid1).style.display == "none") { 
        document.getElementById(divid1).style.display = "block" 
        document.getElementById(divid1).style.visibility = "visible"; 
        var text = document.getElementById("Distribution_Text"); 
        text.setAttribute("class", "newtext1"); 
        text.value = "Distribution Goal"; 

       } 


      } 
     } 



     if (divid == "Added") { 
      if (document.getElementById(divid).style.display == "block") { 
       document.getElementById(divid).style.display == "none" 
       document.getElementById(divid).style.visibility = "hidden"; 
       if (document.getElementById(divid1).style.display == "none") { 
        document.getElementById(divid1).style.display = "block" 
        document.getElementById(divid1).style.visibility = "visible"; 
        var text = document.getElementById("Added_Text"); 
        text.setAttribute("class", "newtext1"); 
        text.value = "Added Value"; 

       } 


      } 
     } 



     if (divid == "Objective") { 
      if (document.getElementById(divid).style.display == "block") { 
       document.getElementById(divid).style.display == "none" 
       document.getElementById(divid).style.visibility = "hidden"; 
       if (document.getElementById(divid1).style.display == "none") { 
        document.getElementById(divid1).style.display = "block" 
        document.getElementById(divid1).style.visibility = "visible"; 
        var text = document.getElementById("Objective_Text"); 
        text.setAttribute("class", "newtext1"); 
        text.value = "Objective"; 

       } 


      } 
     } 

     if (divid == "Strategy") { 
      if (document.getElementById(divid).style.display == "block") { 
       document.getElementById(divid).style.display == "none" 
       document.getElementById(divid).style.visibility = "hidden"; 
       if (document.getElementById(divid1).style.display == "none") { 
        document.getElementById(divid1).style.display = "block" 
        document.getElementById(divid1).style.visibility = "visible"; 
        var text = document.getElementById("Strategy_Text"); 
        text.setAttribute("class", "newtext1"); 
        text.value = "Strategy"; 

       } 


      } 
     } 

     if (divid == "Market") { 
      if (document.getElementById(divid).style.display == "block") { 
       document.getElementById(divid).style.display == "none" 
       document.getElementById(divid).style.visibility = "hidden"; 
       if (document.getElementById(divid1).style.display == "none") { 
        document.getElementById(divid1).style.display = "block" 
        document.getElementById(divid1).style.visibility = "visible"; 
        var text = document.getElementById("Market_Text"); 
        text.setAttribute("class", "newtext1"); 
        text.value = "Market Research"; 

       } 


      } 
     } 



     if (divid == "Refusal") { 
      if (document.getElementById(divid).style.display == "block") { 
       document.getElementById(divid).style.display == "none" 
       document.getElementById(divid).style.visibility = "hidden"; 
       if (document.getElementById(divid1).style.display == "none") { 
        document.getElementById(divid1).style.display = "block" 
        document.getElementById(divid1).style.visibility = "visible"; 
        var text = document.getElementById("Refusal_Text"); 
        text.setAttribute("class", "newtext1"); 
        text.value = "Right of First Refusal Date"; 

       } 


      } 
     } 














    } 
    catch (e) { 
     alert(e); 
    } 
} 




</script> 

在此先感謝 麗思

+3

建議你接受一些更正確的答案...... – 2009-12-08 06:39:38

回答

0

嗯,我想你不能使用Javascript直接插入值到數據庫中,你應該使用一些AJAX來發送數據到服務器端後端。嘗試jQuery Javascript框架,它有很多方便的方法。

0
if (divid == "Refusal") { 
      if (document.getElementById(divid).style.display == "block") { 
       document.getElementById(divid).style.display == "none" 
       document.getElementById(divid).style.visibility = "hidden"; 
       if (document.getElementById(divid1).style.display == "none") { 
        document.getElementById(divid1).style.display = "block" 
        document.getElementById(divid1).style.visibility = "visible"; 
        var text = document.getElementById("Refusal_Text"); 
        text.setAttribute("class", "newtext1"); 
        text.value = "Right of First Refusal Date"; 

       } 


      } 
     } 
     ... 

可以清理到:

function hideVisible(divid, divid1) { 
      var occupations = [{name:'Refusal',text:'Right of First Refusal Date', id:4}, 
           {name:'Market',text:'Market Research'}]; 
      while (var x=0;x<occupations.length;x++){ 
        if (occupations[x]['name'] == divid){ 
         var occupation = occupations[x]; 
         break; 
        } 
      } 
      if (occupation){ 
      if (document.getElementById(divid).style.display == "block") { 
       document.getElementById(divid).style.display == "none" 
       document.getElementById(divid).style.visibility = "hidden"; 
       if (document.getElementById(divid1).style.display == "none") { 
        document.getElementById(divid1).style.display = "block" 
        document.getElementById(divid1).style.visibility = "visible"; 
        var text = document.getElementById(divid+"_Text"); 
        text.setAttribute("class", "newtext1"); 
        text.value = occupation.name; 

       } 

      } 

      post('/newtable', function(xhr) { 
       alert('Saved!: '+xhr.responseText); 
      }, '?id='+occupation.id); 

      } 
      else { 
       //Column not found... 
      } 
     } 
    } 

function post(url, callback, params) { 
    var xhr; 

    if(typeof XMLHttpRequest !== 'undefined') xhr = new XMLHttpRequest(); 
    else { 
     var versions = ["Microsoft.XmlHttp", 
      "MSXML2.XmlHttp", 
      "MSXML2.XmlHttp.3.0", 
      "MSXML2.XmlHttp.4.0", 
      "MSXML2.XmlHttp.5.0"]; 

     for(var i = 0, len = versions.length; i < len; i++) { 
      try { 
       xhr = new ActiveXObject(versions[i]); 
       break; 
      } 
      catch(e){} 
     } // end for 
    } 

    xhr.onreadystatechange = function() { 
     if((xhr.readyState < 4) || xhr.status !== 200) return; 
     callback(xhr); 
    }; 

    xhr.open('POST', url, true); 
    xhr.send(params); 



} 

然後添加一個XMLHTTP請求發佈一些數據到服務器,它會返回一個回調。

+0

非常感謝你的答覆,但我不得不問你的答案:「然後添加一個xmlhttp請求來發布一些數據到服務器,這將返回一個回調。「 請告訴我如何添加xmlHttp請求? – Ritz 2009-12-09 11:42:57

+0

看看帖子功能。 它創建一個xmlhttp對象,然後將數據發送到服務器,並以回調形式返回結果數據。 只需設置該網址即可獲取新列的ID或名稱並在數據庫中創建它。 (上面的javascript對象定義了列的名稱和文本,所以你可以在那裏添加id等。 – CodeJoust 2009-12-09 13:38:37

相關問題