erichexter / twitter.bootstrap.mvc

nuget package to make bootstrap easy with mvc4
Apache License 2.0
248 stars 134 forks source link

ControllerWithContext.tt template bug #57

Closed hujtomi closed 11 years ago

hujtomi commented 11 years ago

Hi,

I think there is a small bug in the ControllerWithContext.tt template in the line 135. I think there is a "Create" instead of "Edit"

Here is the related code part with a comment:

public ActionResult Edit(<#= primaryKey.ShortTypeName #> id = <#= primaryKey.DefaultValue #>) { <# if(isObjectContext) { #> <#= modelName #> <#= modelVariable #> = db.<#= entitySetName #>.Single(<#= lambdaVar #> => <#= lambdaVar #>.<#= primaryKey.Name #> == id); <# } else { #> <#= modelName #> <#= modelVariable #> = db.<#= entitySetName #>.Find(id); <# } #> if (<#= modelVariable #> == null) { return HttpNotFound(); } <# foreach (var property in Model.RelatedProperties.Values) { #> ViewBag.<#= property.ForeignKeyPropertyName #> = new SelectList(db.<#= property.EntitySetName #>, "<#= property.PrimaryKey #>", "<#= property.DisplayPropertyName #>", <#= modelVariable #>.<#= property.ForeignKeyPropertyName #>); <# } #> return View("Create", <#= modelVariable #>); // I think it shoud be "Edit" instead of "Create" }

Thanks, Tomi

serra commented 11 years ago

Thanks for reporting, but that's not a bug, it is intentional. See for instance the sample controller L49 - the same view is used for editing and creating of models. You'll also see that there isn't a default Edit.cshtml in Views\Shared.

I did find another bug in the template though, see #29 for some details.

hujtomi commented 11 years ago

But in the Edit.schtml it generates a hidden filed for the Id of the model @Html.HiddenFor(model => model.Id), but the Create.cshtml won’t do this, so if you post back and you use the Create.cshtml it won’t find the matching element from the database because there is no Id.

serra commented 11 years ago

I see your point. It was chosen to send the id through the action of the form - in the sample application start an edit action and view the generated html and check the action url of the form; it should have the id there.

The default routing matches this to the id parameter of the edit method on your controller, or to a property named 'íd' on your model if you use model binding.

If that sorts it out for you, please close this issue. If not, let me know.