0

I'm a bit new to MVC, and maybe I'm just misunderstanding something, but I can't figure out how to do the following in an elegant way: I have the following Entity that I wan't updated: Model:

public class Entity
    {
        [Key]
        public int Id { get; set; }
        public DateTime Created { get; set; }
        public int FieldInt { get; set; }
        public DateTime FieldDate { get; set; }
        public int FieldOther {get; set; }
    }
}

View: My view displays a bunch textlines with checkboxes attached. The checkboxes are identified by two data-attributes: data-field-int and data-field-date, which is something along the following.

@html.BeginForm("Confirm", "Home", FormMethod.Post) {
...
<input type='checkbox' data-field-int="1" data-field-date="2014/01/01" />
<input type='checkbox' data-field-int="1" data-field-date="2014/02/02" />
<input type='checkbox' data-field-int="1" data-field-date="2014/03/03" />
...
<button id="ConfirmButton" type="submit" class="btn btn-primary">Confirm</button>
}

What I want in the controller is when the ConfirmButton is pressed, create an Entity object for each checkbox that is checked with the value of fieldInt and fieldDate populated with data-field-int and data-field-date attributes respectively.

I can do it by making the controller action take FormCollection as input and by putting a name attribute on the checkboxes with a concatenation of fieldInt and fieldDate and then seperating them in the controller and updating the db. But it seems like there would be a better way, since MVC is so smart with Entity Framework.

I hope you guys can help me understand

Thank you, Peter

2 Answers 2

1

welcome to MVC .

-Using razor engine with model entities is the best practice.

-In the above mentioned code you need to set something like this

@using ( Html.BeginForm("Confirm", "Home", FormMethod.Post))

-As you are new try using strongly typed views with selected templates which generates razor code for you i.e you can analyse deep

-Finally just use model x as parameter to you [HttpPost] action method and convert these entities to you Entity framework entities and save in DB

Additionally : Data attributes are not included in the data that's posted with the form, so there is no way to read them in your controller action. Try using a hidden field instead

Like @Html.HiddenFor(m => m.FieldInt) or <input type="hidden" name="FieldInt" value="1234" />//do similarly for rest

Passing static then @{Model.phoneno = "1234"}

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer, but the thing I don't get is how I can relate a @html.checkboxFor(.. to creating a object in the model where field1 = x and field2 = y
Thank you, I think I'm beginning to get it ☺
1

This question consists of two parts:

1. It is good to specify @model in razor view and use helper methods that take lambda expressions with the model as parameter.

@model MyType

html.Textbox(model => model.FieldOther,...)

Then you create action that takes the model

[HttpPost]
ActionResult MyAction(MyModel model) {
 ....
}

Mvc will try to create instance of the model and map form fields to the model properties.

2.

You can use entity as model but, believe me, so called Data transfer Objects and/or View Models were created for a reason and as application evolves, single views evolve too to manipulate data from many related database entities.

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.