1

I have a Model as below:

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

and I have an ActionResult which is like this:

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

now, I want to fill Start property from another external javascript file using ajax like this:

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

How can I access to another View TextBox and fill that using ajax? What should I do in data on ajax?

  • Possible duplicate of [How to send a model in jQuery $.ajax() post request to MVC controller method](http://stackoverflow.com/questions/1518417/how-to-send-a-model-in-jquery-ajax-post-request-to-mvc-controller-method) – Ram Singh Dec 06 '16 at 11:00
  • `data: { Start: $(yourTextBox).val() },` –  Dec 06 '16 at 11:02
  • @StephenMuecke thanks for your reply but in `ActionResult` it get `model`.how can i access to model `property`? – mojtaba1390 Dec 06 '16 at 11:05
  • @mojtaba1390 model.Start .. – Ram Singh Dec 06 '16 at 11:07
  • Sorry, Not sure I understand your comment - if the value of the textbox is (say) 12/6/2016, then `model.Start` will be 12/6/2016 –  Dec 06 '16 at 11:07
  • @StephenMuecke unfortunately it does not work – mojtaba1390 Dec 06 '16 at 11:21
  • Of course it works. –  Dec 06 '16 at 11:22
  • javascript file did not recognize `model.Start` because i have not any model in my `javascript` file.i want just access to that property without model. – mojtaba1390 Dec 06 '16 at 11:29
  • What do you mean _model.Start_? You have said you want the value of a textbox! –  Dec 07 '16 at 22:56

1 Answers1

0

Please try below code:-

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);
}
Sarika Koli
  • 773
  • 6
  • 11