在mvc中通过javascript在数据库sql server 2008中插入值/
问题描述:
我想在我的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>
在此先感谢 丽思
答
嗯,我想你不能使用Javascript直接插入值到数据库中,你应该使用一些AJAX来发送数据到服务器端后端。尝试jQuery Javascript框架,它有很多方便的方法。
答
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请求发布一些数据到服务器,它会返回一个回调。
建议你接受一些更正确的答案...... – 2009-12-08 06:39:38