getting dropdown's text in Post action method - asp.net-mvc

I have a Razor view in my asp.net MVC3 application with a dropdownlist like this:
#Html.DropDownListFor(model => model.Account.AccountType, new SelectList(Model.AccountTypes, "AccountTypeCode", "Abbreviation"))
This dropdown is inside a form. When form is posted to action method and viewmodel is filled because of model binding, It get the value(AccountTypeCode) and not the text "Abbreviation" property of dropdownlist. I want to get both of these. how can I get these in post action method.
Please suggest.

If you need more than one property of an object as a value for a dropdown, the easiest way is to create a combination of these in a partial class
EF 4, how to add partial classes this quesion should help you. You will be able to combine values under one property that you will provide to your dropdown helper.
If you don't want to use partial classes I would advise creating your own helper, that will be a lot easier than trying to use something that does not fit your needs. You can do something like :
#helper CustomDropdown(string name, IEnumerable<AccountTypes> valueList)
{
<select>
#foreach (var item in valueList)
{
<option value="#item.Abbreviation #AccountTypeCode">#item.Abbreviation</option>
}
</select>
}
Google "Creating an Inline HTML Helper" to get some valuable resources on that topic

I struggled with this recently, and the best I could do was:
ViewBag.Message = myModel.myProperty.ToString().
in the controller action. Assuming myProperty is AccountType.
Then in my view I just did
#ViewBag.Message
The next problem I encountered is that it spit out the exact text, without spacing. I had to use a helper to add spacing (but it was based on each word being capitalized, so "ThisIsSomeText" would show up as "This Is Some Text".

Related

How to show the value of model in Kendo Custom Editor Template?

I'm using a scheduler of Kendo UI for MVC. I created a Custom Editor Template. It works fine. When click the schedule, it pops a windows and show the info.
I added some properties in the model, the value passed to the editor template. Now, the question is:
How can I show the added properties (just as value, and I do not want to change it) in the editor template popup?
I found that if I use a textbox:
#(Html.TextBoxFor(model => model.Role, new { #class = "k-textbox" }))
It shows a textbox and shows the correct value of Role. However, if I use
<div>#(Model.Role)</div>
It shows blank, just as the value is NULL.
I actually want to show several added properties as a sentence, i.e. I added Role and UserName and I ant to show something like:
UserNameValue has Role of RoleValue
Anyone knows how to do it?
I use this as sample in the code:
http://www.telerik.com/support/code-library/custom-editor
Thanks
Your solution is to put this line because the editor template is binded by mvvm
<div data-bind="text: Role"></div>
Docs
In the code: model => model.Role model is just an alias, not an actual object. You could replace it with m => m.Role and it would function the same.
When you write #Model in your view you are using the object of ContractViewModel which is passed from Controller action, if it is not passed from View it can be null and accessing any property of Model can throw Null Reference Exception and writing Model.Contractors you basically mean ContractViewModel.Contractors
See this post: mvc uppercase Model vs lowercase model

MVC Create method data flow

I think i know some of the basics of MVC but there's one thing which I don't understand yet.
In the view Create which is generated automatically when you set up your project, how is data sent to the controller? I'm used to seeing ActionLinks with parameters but here there's no actionLink so I can't understand how data travel from the view to the controller.
Could you explain it to me please?
as you know, in your view, the very first line (usually) tells the view, about the Model being used within this view. like:
#model Models.CarViewModel
lets suppose, you have a form on this view, and it is posted to some action called Edit. Then you must have your Edit action, expecting the parameter of type you used as model in your view. like:
[HttpPost]
public ActionResult(CarViewModel model)
{
//logic
}
This convention is known as Strongly Typed View. Suppose Then you have some textbox for a property Name of your model as:
#Html.TextBoxFor(x => x.Name)
when the form is posted to Edit Action, the variable model in parameter of Edit action will be holding the respective values. i.e, model.Name

Using multiple partial views in MVC3 razor forms

I have an insurance entry form that has contact information for two people. I have created a Razor partial view for the contact entry and put it in the form twice. The 'master' view model (VmApplicationForm) contains two instances of a subsidiary view model (VmPolicyHolder) corresponding to the two contacts as well as some properties common to both contacts. I am calling #Html.RenderPartial("_CreateOrEdit", Model.contactInfo1) and #Html.RenderPartial("_CreateOrEdit", Model.contactInfo2) in the page. With this arrangement (no surprises) the rendered code has duplicate IDs for the form input elements.
Is there any way of getting RenderPartial to prefix the IDs and Name attributes? I couldn't see this in the documentation, but perhaps I've missed something.
Sorry, I don't have time yet to give you the example code, but i'll give you the idea. You should first create EditorTemplate for that probably called ContactInfo class. And then, in the base class(Holding that two contacts) edit view, you should write
#Html.EditorFor(model => model.contactInfo1)
#Html.EditorFor(model => model.contactInfo2)
This way, it will render that EditorTemplate and generate correct ids and names to inputs within it.
What you are doing is trying to post a collection of items in a form- this can indeed be done in MVC (as well as in any web page/application that uses a FORM tag), however it requires some special handling to avoid id collisions and to correctly format the post data. Steve Sanderson has a great post on how to accomplish this:
http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
Essentially its a wrapper to append unique guids to element ids (in your case for each contactInfo), and create the proper array format in the tags. eg <input name="ContactInfo[f2cc4d6b-fc32-45e9-9d3d-fce54c3fede3].FirstName">
if your model is something like ContactInfo, you will end up posting like
[HttpPost]
public ActionResult Index(IEnumerable<ContactInfo> contacts)
{
// To do: do whatever you want with the data
}

Problem with IEnumerable in View

I have a view that enumerates over a model.
Outside of the grid that enumerates over the model, I want to have a create link that accepts a parameter of MeetingActionId, that will bind the ActionUpdate object to a specific MeetingAction.
How do I get the create link to accept that property? at the moment i get the error
'System.Collections.Generic.IEnumerable' does not contain
a definition for 'MeetingActionId' and no extension method 'MeetingActionId'
accepting a first argument of type
'System.Collections.Generic.IEnumerable' could be found
I assume it's something to do with the IEnumerable. I was going to solve it by putting the grid in a partial, but that seems like a hacky solution to me.
Can anyone assist, and hopefully educate me as to why this is happening?
Thank you.
The code snippet below is using the MVCContrib Grid, and T4MVC for strongly typed action links.
#model IEnumerable<Actioner.Models.ActionUpdate>
#using MvcContrib.UI.Grid
#{
ViewBag.Title = "ListUpdates";
}
<h2>ListUpdates</h2>
#Html.ActionLink("Add New Update",MVC.ActionUpdates.Create(Model.MeetingActionId))
#Html.Grid(Model).Columns(column => {
column.For(a=> a.UpdateText);
column.For(a=> a.CreatedOn).Format("{0:d}");
column.For(a=>a.CreatedBy);
})
EDIT: Thanks for the contributions guys. After some thinking i decided it would be better to have the grid of actionupdates rendered as a partial view within the 'details' view of the MeetingActions, thus avoiding this issue entirely.
This question could be closed
Hi
I'm not sure that I understand this correctly but it seems like the Model is a list that contains items of type ActionUpdate, that type has a property called MeetingActionId. If that is the case the Model itself does not have the property MeetingActionId that you are trying to use. Perhaps you could to this:
Model.First().MeetingActionId

ASP.NET MVC - How to get checkbox values on post

Hey all, this is a newbie ASP.NET MVC question.
I have a view that has a list of checkboxes and a submit button. On submit it posts to a controller method but I can't figure out how to get the values of the checkboxes. Also, I can't figure out how to get model data that I passed into the view when I'm in the post method, I tried using Html.Hidden but that didn't seem to work.
Here's the code:
http://pastebin.com/m2efe8a94 (View)
http://pastebin.com/m39ebc6b9 (Controller)
Thanks for any input received,
Justin
First thing I noticed is that your hidden fields need to be inside your form. Currently in your view, they are above the BeginForm, so they won't be included in the form submission.
To get the values of the selected check boxes, add an IsOffered parameter to your OfferTrade Action method.
public ActionResult OfferTrade(FormCollection result, List<string> IsOffered)
That parameter will contain a list of the ItemId's for all the checked IsOffered boxes.
The HtmlHelper's CheckBox works differently and I don't like the way it works, so I don't use it.
Making the IsOffered parameter type List<int> should also work if your ItemId field is an integer.
First of all your ItemId and UserId is outside your form:
<%= Html.Hidden("ItemId", Model.ItemIWant.ItemId) %>
<%= Html.Hidden("UserId", Model.ItemIWant.UserId) %>
//...
<% using (Html.BeginForm()) {%>
Secondly you could try to make your Controller action method use "model binding" (if this is also called model binding)
public ActionResult OfferTrade(int ItemId, int UserId, IList<string> IsOfferred)
Edit Just noticed you are not using the HtmlHelper CheckBox so your list will contain only selected items, but a point still:
You might want to look into Phil Haacks post on Model Binding To A List, but there is a small change to this in the RTM version of MVC:
You dont need the ".Index" hidden fields, but then the indexes in the Name fields must be zero-indexed and increasing (by 1).

Resources