MVC 5.1.2 EditorFor htmlAttributes - editorfor

This looks simple enough:
#Html.EditorFor(m =>m.FirstName, new { htmlAttributes = new { #class = "form-control" }, }
however, the emitted page does not have this class applied.

#Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { #class = "form-control" } })
this seems to work for me, you have a comma at the end there and failed to close the paranthesis correctly.

The MVC Editor helper is expecting an additionalViewData object as that argument. This has been better handled in ASP.Net MVC 5.1 but for the time being, swapping your EditorFor to a TextBoxFor should do the trick nicely.

Related

EF4.3 linking viewbag item to viewmodel

I have the following action method in my Tournament controller:
public ActionResult Edit(int id) {
EditTournamentViewModel viewModel = new EditTournamentViewModel {
Tournament = _tournamentService.GetTournamentByID(id),
TournamentDivisions = _divisionService.GetTournamentDivisions(id).ToList()
};
ViewBag.SportID = new SelectList(_sportService.GetSports(), "SportID", "Name");
ViewBag.CountryID = new SelectList(_countryService.GetCountries(), "CountryID", "Name");
return View(viewModel);
}
The two viewbag items are select lists which will be filled up. When I inspect the page with firebug the name of the SelectList for ViewBag.SportID is SportID, which is what I would expect. But I want the value that is selected there to be entered in the SportID property of the Tournament property in my viewModel. So the name should be Tournament.SportID.
I don't understand how I can achieve this, I wasn't able to change the SelectList's name in Razor like this:
<div class="editor-field">
#Html.DropDownList("SportID", "Select a sport", new { name = "Tournament.SportID" } )
#Html.ValidationMessageFor(t => t.Tournament.Sport)
</div>
This is the code I have in my view:
<div class="editor-field">
#Html.DropDownList("SportID", "Select a sport")
#Html.ValidationMessageFor(t => t.Tournament.Sport)
</div>
A solution I could take is instead of having a Tournament property in my viewmodel, I could copy over the individual properties and then use automapper but I would like to hear if there is a fix without having to do that (unless of course what I am doing is considered very bad practice).
Did you try
#Html.DropDownListFor(m => m.Tournament.SportID, ViewBag.SportID, "Select a sport")
?
(if you need the acual value of Tournament.SportId, for an update, for example, you can just look at the different constructors of SelectList and change it in your "ViewBag creation")
CORRECTION :
SelectList sportList = ViewBag.SportID;
#Html.DropDownListFor(m => m.Tournament.SportID, sportList, "Select a sport")

ASP .NET MVC 2 - How do I pass an object from View to Controller w/ Ajax?

I have an object MainObject with a list of objects, SubObjects, among other things. I am trying to have the user click a link on the View to add a new SubObject to the page. However, I am unable to pass the MainObject I am working with into the Action method. The MainObject I currently receive is empty, with all its values set to null. How do I send my controller action the MainObject that was used to render the View originally?
The relevant section of the view looks like this:
<div class="editor-list" id="subObjectsList">
<%: Html.EditorFor(model => model.SubObjects, "~/Views/MainObject/EditorTemplates/SubObjectsList.ascx")%>
</div>
<%: Ajax.ActionLink("Add Ajax subObject", "AddBlanksubObjectToSubObjectsList", new AjaxOptions { UpdateTargetId = "subObjectsList", InsertionMode = InsertionMode.Replace })%>
The relevant function from the controller looks like this:
public ActionResult AddBlanksubObjectToSubObjectsList(MainObject mainobject)
{
mainobject.SubObjects.Add(new SubObject());
return PartialView("~/Views/MainObject/EditorTemplates/SubObjectsList.acsx", mainobject.SubObjects);
}
I ended up with the following:
View:
<div class="editor-list" id="subObjectsList">
<%: Html.EditorFor(model => model.SubObjects, "~/Views/MainObject/EditorTemplates/SubObjectsList.ascx")%>
</div>
<input type="button" name="addSubObject" value="Add New SubObject" onclick="AddNewSubObject('#SubObjectList')" />
Control:
public ActionResult GetNewSubObject()
{
SubObject subObject= new SubObject();
return PartialView("~/Views/TestCase/EditorTemplates/SubObject.ascx", subObject);
}
And, finally, I added this JQuery script:
function AddNewSubObject(subObjectListDiv) {
$.get("/TestCase/GetNewSubObject", function (data) {
//there is one fieldset per SubObject already in the list,
//so this is the index of the new SubObject
var index = $(subObjectListDiv + " > fieldset").size();
//the returned SubObject prefixes its field namess with "[0]."
//but MVC expects a prefix like "SubObjects[0]" -
//plus the index might not be 0, so need to fix that, too
data = data.replace(/name="\[0\]/g, 'name="SubObject[' + index + "]");
//now append the new SubObject to the list
$(subObjectListDiv).append(data);
});
}
If someone has a better way to do this than kludging the MVC syntax for nested objects onto a returned View using JQuery, please post it; I'd love to believe that there is a better way to do this. For now, I'm accepting my answer.

Creating a generic edit view with ASP.NET, MVC, and Entity Framework

In my database, I have 40 tables that contain only an ID number and a name. My database is accessed using Entity Framework. While I have no trouble editing them each by generating a strongly-typed view and postback methods for each object, I would like to create a more generic method and view for viewing and editing these objects.
I am currently using the following code to access each object. In this case, it is for an object of 'AddressType':
public ActionMethod EditAddressType(int ID)
{
var result = database.AddressType.Single(a => a.ID == ID);
View(result);
}
[HttpPost]
public ActionMethod EditAddressType(int ID, FormCollection formValues)
{
var result = database.AddressType.Single(a => a.ID == ID);
UpdateModel(result);
database.SaveChanges();
return View("SaveSuccess");
}
The view 'EditAddressType' is strongly typed and works fine, but there's a lot of repeated code (one instance of this for each object). I've been told that I need to use reflection, but I'm at a loss for how to implement this. My understanding is that I need to retrieve the object type so I can replace the hardcoded reference to the object, but I'm not sure how to get this information from the postback.
I've had success binding the information to ViewData in the controller and passing that to a ViewPage view that knows to look for this ViewData, but I don't know how to postback the changes to a controller.
Thanks for any help you can give me!
If you are going to edit the object you don't need to refetch it from the database in your POST action. The first thing would of course be to abstract my data access code from the controller:
public class AddressesController: Controller
{
private readonly IAddressesRepository _repository;
public AddressesController(IAddressesRepository repository)
{
_repository = repository;
}
public ActionMethod Edit(int id)
{
var result = _repository.GetAddress(id);
return View(result);
}
[HttpPut]
public ActionMethod Update(AddressViewModel address)
{
_repository.Save(address);
return View("SaveSuccess");
}
}
You will notice that I have renamed some of the actions and accept verbs to make this controller a bit more RESTFul.
The associated view might look like this:
<% using (Html.BeginForm<AddressesController>(c => c.Update(null))) { %>
<%: Html.HttpMethodOverride(HttpVerbs.Put) %>
<%: Html.HiddenFor(model => model.Id) %>
<%: Html.TextBoxFor(model => model.Name) %>
<input type="submit" value="Save" />
<% } %>
As far as the implementation of this IAddressesRepository interface is concerned, that's totally up to you: Entity Framework, NHibernate, XML File, Remote Web Service call, ..., that's an implementation detail that has nothing to do with ASP.NET MVC.

Access views url function in Zend_Form

I have some Forms that for some reasons have to be instantiated in the different modules bootstraps. But in those forms I want to use
$this->setAction($this->getView()->url(array('controller' => 'foo', 'action' => 'bar')));
in the constructor. But since the viewhelper url is not accessible yet since I'm in the bootstrap, is there anyway around this? What I get now is
Fatal error: Uncaught exception 'Zend_Controller_Router_Exception' with message 'Route default is not defined'
on that line. I have NO custom routes so I only use the default router and routes.
If you really need to instantiate the forms so early, then perhaps one possibility is the order of the initializations in your Bootstrap.
In the Bootstrap method where you instantiate your forms, just make sure that you bootstrap the view first. Then grab the view object and pass it to the form during instantiation.
Something like:
protected function _initForms()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$form = new My_Form($view);
// ...
}
Then in your form, you can do something like:
public function __construct($view)
{
$this->setView($view);
}
public function init()
{
// set the action
$this->setAction($this->getView()->url(array(
'controller' => 'foo',
'action' => 'bar',
)));
// create your form elements
// ...
}
Whaddya think?
I decided to change my approach and made the class that manages all the forms accept strings of forms instead of the forms.
static public function registerForm($formClassName, $formName) {
self::$_forms[$formName] = $formClassName;
}
Then I created function to get a form or all forms from the class like so
public function getForm($name) {
if(empty(self::$_forms[$name])) {
throw new Core_Form_Store_Exception;
}
// If the for is instanciated we return it
else if(self::$_forms[$name] instanceof Zend_Form) {
return self::$_forms[$name];
}
else {
// Instanciate the class and return it
$form = new self::$_forms[$name];
self::$_forms[$name] = $form;
return $form;
}
}
getForms() only calls getForm() for each available form. I tired to mimic the interface of the normal Zend_Form a bit when it comes to the names of functions and the order arguments are passed. Now I have it working and as a bonus I don't actually instantiate any form until I need it. And if I only request a specific form from the store class only that form is instantiate and saved for future access.
How about you set the action from the view when rendering the form, eg
<?php echo $this->form->setAction($this->url(array(
'controller' => 'foo',
'action' => 'bar'
))) ?>

actionlink not resolving urls related to route

I have the following route ( it's the first in my global.asax )
routes.MapRoute(
"AdminCompany", // Route name
"{controller}.aspx/{action}/{companyId}/{id}", // URL with parameters
new { controller = "Home", action = "Index", companyId = "", id = "" } // Parameter defaults
);
if i navigation to
"Order/DisplayAdmin/2/79000180" it resolves correctly
However if i do the following
Html.ActionLink("View", "DisplayAdmin", new {companyId = Model.CompanyId, id = order.OrderNumber }, new { #class = "button add" })
it displays
/Order.aspx/DisplayAdmin/39068760?companyId=0
which also works, but isn't so pretty :)
Here is my Controller Method
public ActionResult DisplayAdmin(int companyId, [DefaultValue(0)]int id, [DefaultValue(0)] int orderItemStatusId)
{
var viewModel = DisplayAdminViewModel(companyId, id, _statusResponses);
return View(viewModel);
}
Am i calling ActionLink the wrong way? how do i get the nice Urls?
only thing I can think of that is happening is that its falling back to the Default route, I did a copy paste of both your route and the html.ActionLink and it works perfectly for me displaying it like "/Order.aspx/DisplayAdmin/39068760/45456", i did replicate the same fault like you get if the naming isn't the same in the route and action link.
Use the overload of ActionLink that has a RouteValueDictionary argument.
It appears that you are currently using the overload with the "object" argument and it's going to a workable, but not as clean, url.