在.NET MVC

时间:2016-10-14 18:41:57

标签: c# asp.net asp.net-mvc razor

我的剃刀视图中有一个DropDownList,它从我的模型中获取值,该值来自Data Base。我想要实现的是当从DropDownList中选择一个选项并单击一个按钮时,显示一个在另一个下面选择的选项列表,所有这些选项都在同一个视图中,每个项目旁边都有一个用于编辑或删除的链接。然后在另一个屏幕中,我可以添加这个最终的"列表"到我的数据库。这是我的代码的一部分:

模型

public class SchedulerViewModel
{        
        public string Frequency { get; set; }     
        public string SelectedReportValue { get; set; }
        public IEnumerable<SelectListItem> ReportList
        {
            get
            {
                List<string> reportlist = DataAccess.retrieveReportList();
                return new SelectList(reportlist);
            }
        }

        public string NewCronName { get; set; }

        [Display(Name = "OrderDate")]
        [DataType(DataType.Date)]
        public System.DateTime OrderDate { get; set; }

        public IEnumerable<SelectListItem> FrequencyList
        {
            get
            {
                List<string> frequencyList = DataAccess.retrieveFrequencyList();
                return new SelectList(frequencyList);
            }
        }
}

控制器

[HttpGet]
public ActionResult AddCron()
{                  
    return View();            
}

[HttpPost]
public ActionResult AddCron(SchedulerViewModel model)
{            
    return View(model);
}

查看

<script>
    function myFunction() {
        $("#div1").load('@Html.LabelFor(model => model.SelectedReportValue)');

    }
</script>
.
.
.


<tr>
    <td><h5>Frequency: </h5></td>
    <td> @Html.DropDownListFor(model => model.Frequency, Model.FrequencyList)</td>

    <td><h5>First Run Date: </h5></td>        
    <td> @Html.TextBoxFor(m => m.OrderDate, new { @class = "form-control datepicker", @id = "datepicker" })</td>        
</tr>
<tr class="blank_row">
                &nbsp;
</tr>
<tr>
    <td><p><b>Add Report</b></p></td>
</tr>
<tr>
    <td><h5>Report :</h5></td>
    <td>@Html.DropDownListFor(model => model.SelectedReportValue, Model.ReportList)</td>
    <td><input type="submit" name="Add" value="Add" onclick="myFunction()" /></td>
</tr>

<div id="div1"></div>

我可以在[HttpPost]方法中看到我的模型中返回的选定值,但仍然无法使其在视图中工作。有什么建议吗?

enter image description here

这就是我想要的......

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试摆脱jquery,只需使用razor:

<div id="div1">
  @Html.LabelFor(model => model.SelectedReportValue)
</div>

您的评论基础我建议您可以使用jquery触发更改事件并手动将项目添加到列表中:

$( "#filetype" ).change(function() {
  //code handler to add to list
});