我正在开发一个搜索媒体的网络应用程序,所以对于我的搜索界面,我应该有这样的东西:用户指定4个字段,他可以添加其他字段(相同的字段性质,但他想添加另一个标准)点击图标Plus,我用jquery完成了,见下图:enter image description here
但我的问题是:我怎样才能将表单值的监听传递给我的动作!! 在这里,你是我的控制器的代码和我的观点(我只为一个人做) 控制器:
[HttpPost]
public ActionResult RechercheForm(Recherche FormData)
{
List<Asset> liste = null;
if (FormData.Op == "Like")
{
if (FormData.Critere == "Titre")
{
liste = bdd.Assets.Where(a => (a.Titre.Contains(FormData.Val1)
)).ToList();
ViewBag.Val = FormData.Val1;
return View("Index",liste);
}
else
if (FormData.Critere == "TitreCourt")
{
liste = bdd.Assets.Where(a => (a.TitreCourt.Contains(FormData.Val1)
)).ToList();
return View("Index", liste);
}
}
return View("Index");
}
查看:
<div class="right-content" style="width: 760px;" >
<!-- DropListDownData -->
@{
Dictionary<string, string> ListCritere=new Dictionary<string, string>
{
{"Titre", "Titre"},
{"TitreCourt", "TitreCourt"},
// some lines skipped
};
Dictionary<string, string> ListOp = new Dictionary<string, string>
{
{"Like", "Like"},
{"Sup", "Sup"},
{"Inf", "Inf"},
// some lines skipped
};
}
@using (Html.BeginForm("RechercheForm", "Recherche",new { ReturnUrl = ViewBag.ReturnUrl },FormMethod.Post, new { @class = "form-inline" })){
<button type="submit" class="btn btn-default">Rechecrcher</button>
<table id="TableRech">
<tr>
<th>Critere</th>
<th>Opérateur</th>
<th>Valeur1</th>
<th>Valeur2</th>
<th></th>
</tr>
<tr>
<td><div class="form-group">
@Html.DropDownList("Critere", new SelectList(ListCritere, "Key", "Value"),new { @class = "form-control" })
</div></td>
<td><div class="form-group">
@Html.DropDownList("Op", new SelectList(ListOp, "Key", "Value"),new { @class = "form-control" })
</div></td>
<td><div class="form-group">
@Html.TextBox("Val1",null,new {id = "Val2", @class = "textbox", style="width:50px;padding-right: 50px; " })
</div></td>
<td> <div class="form-group">
@Html.TextBox("Val2",null,new {id = "Val2", @class = "textbox", style="width:50px;padding-right: 50px; " })
</div></td>
<td><span class="glyphicon glyphicon-plus-sign" id="Plus" style="width: 15px;" onclick="RechFunctionPlus()" ></span></td>
</tr>
</table>
}
</div>
</div>
<script>
function RechFunctionPlus() {
var table= document.getElementById('TableRech');
var tr = document.createElement('tr');
table.appendChild(tr);
var td = document.createElement('td');
td.innerHTML='<select class="form-control" id="Critere" name="State"> <!-- some attrs=ibutes skipped --><option value=""></option><option value="Titre">Titre</option><option value="TitreCourt">TitreCourt</option><option value="Type">Type</option></select>';
tr.appendChild(td);
var td2 = document.createElement('td');
td2.innerHTML='<select class="form-control" id="Op" name="State"> <!-- some attrs=ibutes skipped --><option value=""></option><option value="Like">Like</option><option value="Inf">Inf</option><option value="Sup">Sup</option></select>';
tr.appendChild(td2);
var td3 = document.createElement('td');
td3.innerHTML='@Html.TextBox("Val1",null,new {id = "Val1", @class = "textbox", style="width:50px;padding-right: 50px; " })';
tr.appendChild(td3);
var td4 = document.createElement('td');
td4.innerHTML='@Html.TextBox("Val2",null,new {id = "Val2", @class = "textbox", style="width:50px;padding-right: 50px; " })';
tr.appendChild(td4);
var td5 = document.createElement('td');
td5.innerHTML='<span class="glyphicon glyphicon-minus-sign" id="Plus" style="width: 15px;" onclick="RechFunctionMoins()" ></span>';
tr.appendChild(td5);
}
</script>
我想做一个名为“Recherche”的列表,我将把它传递给我的动作“RechercheForm”,但我不知道如何?!!
答案 0 :(得分:0)
使用JavaScript
例如:
function sendForm(projectId, target) {
$.ajax({
url: target,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
projectId: projectId,
userAccountIds: [1, 2, 3]
}),
success: ajaxOnSuccess,
error: function (jqXHR, exception) {
alert('Error message.');
}
});
}
请参阅此帖子Post JavaScript array with AJAX to asp.net MVC controller