控制器:
{
private static string selection = String.Empty;
dynamic mymodel = new ExpandoObject();
public ActionResult Post(string Name)
{
selection = Name;
return RedirectToAction("Index");
}
public ActionResult Index()
{
SegmentRepository segment = new SegmentRepository();
mymodel.listofSegments = segment.GetSegmentation();
DynamicRepository dynamic = new DynamicRepository();
mymodel.listofDynamic = dynamic.GetDynamicContent(selection); //After selecting the segmentation in the view it returns the required dynamic content in mymodel.listofDynamic but does not display it in the view.
return View(mymodel);
}
}
在视图中选择细分后,它会在mymodel.listofDynamic
中返回所需的动态内容,但不会在视图中显示。
查看:
<script>
function seg() {
var employment = document.getElementById("Employment").value;
$.ajax({
type: "POST", //HTTP POST Method
url: '@Url.Action("Post","Home")', // Controller/View
data: {
//Passing data
Name: employment //Reading text box values using Jquery
}
})
}
</script>
<tr>
<td height="100">
<label>220</label>
</td>
<td>
<select id="Employment">
<option>---Select---</option>
@foreach (var item in Model.listofSegments)
{
<option name="selectedSegment" value="@item">@item</option>
}
</select>
<input type="submit" value="Send" name="submit" onclick="seg()">
</td>
<td>
<select name="Dynamic">
<option>---Select---</option>
@foreach (var item in Model.listofDynamic)
{
<option name="selectedDynamic" value="@item">@item</option>
}// I need the data to get listed here
</select>
<input type="submit" value="Send" name="Submit1">
</td>
</tr>
我需要public ActionResult Index()
方法再次运行,以便listofDynamic
中的数据打印在视图中。我怎样才能做到这一点?
答案 0 :(得分:0)
在Ajax
的数据部分中,使用以下内容:
data: { "name": employment }, //Pass value of employment variable as the name parameter
success: function(data) {
alert(data); //In the success function, pass the data to be notified in the view
}
以上内容将使用Ajax
将数据从控制器返回到视图。
同时,使用Json
方法使其正常工作或ActionResult
也可以:
public JsonResult GetResults(string name) //Here is the name parameter
{
MainEntities db = new MainEntities(); //Db context
var con = (from c in db.TableName
where c.ColumnName == name //Pass the name parameter in the query
select c).ToList();
return Json(con); //Finally returns the result in Json
}
答案 1 :(得分:0)
如果您想通过Ajax获取值,您会忘记成功和错误部分。 查看部分:
$.ajax({
type: "POST", //HTTP POST Method
url: '@Url.Action("Post","Home")', // Controller/View
data: {
//Passing data
Name: employment //Reading text box values using Jquery
},
success:function(result){alert(result);},
error:function(error){alert(error.responseText);}
})
像AT-2017这样的控制器端写了JsonResult,或者你可以返回部分视图/任何其他类型。