-1

I have model, which use by EntityFramework

public class Goal
{
    public int ID { get; set; }
    public string GoalName { get; set; }
    public int GoalTarget { get; set; }        
}

My Jscript code

    var GoalName = $('input[name="GoalName[]"]').serialize();
    var GoalId= $('input[name="GoalId[]"]').serialize();
    alert(GoalName);
    $.post('/Home/UpdateGoals', { GoalName: GoalName, GoalId: GoalId}, function (data) {
    });

Question is - how to correctly receive data from client to controller? Use form['paramname'] and next save db or use special created ViewModel for receiving data and next save db ?

For example I should create new modelView

class GoalView
{
     public string[] GoalName { get; set; }
     public string[] GoalId { get; set; }
}

and now i controller I can foreach data from this model and save db ?

    public ActionResult UpdateGoals(GoalView goalview)
    {

           how I can save data from goalview to db.Goals (which using Goal model) ??
        return Content();
    }

Could you please advise how to do correctly and provide some examples ?

Fullbalanced
  • 87
  • 11
  • 1
    Unclear what your trying to do here. Is you view generating inputs for multiple `Goal`? –  Dec 12 '16 at 11:08
  • I have HTML form with dinamically inputs fields (user can remove and add). I need to pass this data via AJAX request to controller and make changes in DB. Question is - how I can make changes in db (just please provide a simple example) and is it ok to use it as it was described above, i mean that possible something is incorrect and needs to be done by another way ? – Fullbalanced Dec 12 '16 at 11:10
  • What your doing is not the correct approach. Generate you view correctly so it binds to `List` - refer [this answer](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) for options –  Dec 12 '16 at 11:12
  • How to do if fields from my Goal model and form, which sending parameters - are different? – Fullbalanced Dec 12 '16 at 11:14

1 Answers1

0
var filter={
GoalName : $('input[name="GoalName[]"]').serialize();
GoalId=: $('input[name="GoalId[]"]').serialize();
}



 $.post('/Home/UpdateGoals', $.param(filter), function (data) {
    });

ref : http://api.jquery.com/jquery.param/

go..
  • 958
  • 7
  • 15