0

I'm kind new in the MVC world and I am stucked on a situation that seems pretty simple.

I have a model named Company, and each Company can have many Contacts (name and phone).

What I want is to display a first set of fields for a Contact and a Add Another link that adds the html fields for a new contact.

How can I do that? I know that I can create the fields using JS but how can I send this to my controller? Is there a MVC-way yo do that?

I'm using EF as well

2
  • What kind of ORM are you using? Dapper? Hibernate? Straight SQL? The answer will depend slightly. The simple answer is to have a CompanyId field in each contact. Commented Jan 28, 2014 at 1:35
  • 2
    In general you can bind elements from your html form to your controller actions parameters by name. So if you add new fields with js insert them with propper names (little harder with collections) and implement a POST method for that form that updating your model. Take a look here: haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx Commented Jan 28, 2014 at 1:43

1 Answer 1

1

you can use ajax calls to talk to your controllers through js.

jQuery

var controlName = '/YourControlerName/';
$.getJSON(controlName + 'GetTelss', 
       function (data)`
{
  /// update your ui   
});

});

MVC

public ActionResult GetTels() 
{     
  return Json(yourDAta,JsonRequestBehavior.AllowGet);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.