如何在mvc中将变量传递给模型的属性

时间:2016-12-06 10:58:16

标签: javascript jquery ajax asp.net-mvc-5

我有一个模型如下:

public  class Class1
{
    public int Id { get; set; }
    public DateTime Start { get; set; }
} 

我有ActionResult,就像这样:

public ActionResult Create(Class1 model)
{
    ...
} 

现在,我想使用Start来填充另一个外部javascript文件中的ajax属性,如下所示:

$.ajax({
    url: "/Admin/Create",
    dataType: "Json",
    type: "Post",
    data: Start:"..."
});

如何访问另一个View TextBox并使用ajax填充?我应该怎么做关于ajax的数据?

1 个答案:

答案 0 :(得分:0)

请尝试以下代码: -



var model = { Name :"Shyju", 
              Location:"Detroit", 
              Interests : ["Code","Coffee","Stackoverflow"]
            };

$.ajax({
    type: "POST",
    data: JSON.stringify(model),
    url: url,
    contentType: "application/json"
}).done(function (res) {
    $("#SomeDivToShowTheResult").html(res);
});



public class DashboardViewModel
{
  public string Name {set;get;}
  public string Location {set;get;}
  public List<string> Interests {set;get;}
}


[HttpPost]
public PartialViewResult IndexPartial([FromBody] DashboardViewModel m)
{
    return PartialView("_IndexPartial",m);
}
&#13;
&#13;
&#13;