I have a MVC5 project, I have First Name and Last Name as 2 separate textboxes. I need to combine these 2 and shows as one textbox as Customer Name how I can do that?
This is what I have now that shows 2 text boxes:
<div class="form-group">
#Html.LabelFor(model => model.First_Name, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.First_Name, new{disabled = "disabled" })
#Html.ValidationMessageFor(model => model.First_Name)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Last_Name, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.Last_Name, new{disabled = "disabled" })
#Html.ValidationMessageFor(model => model.Last_Name)
</div>
</div>
If the fields are truly combined then you'd add a property to your model representing the new single field:
public string CustomerName { get; set; }
and use it in your view:
#Html.TextBoxFor(model => model.CustomerName, new{disabled = "disabled" })
#Html.ValidationMessageFor(model => model.CustomerName)
(Though if, when saving back to the server, you need to parse the values back out into two separate fields then that can get tricky. Don't make too many assumptions about names. But if you must, then that parsing should likely happen in the setter for this property and the getter should dynamically display the concatenated values as below.)
If, on the other hand, it should be a read-only display of the combined values, you'd create a read-only property to view the other values:
public string CustomerName
{
get { return string.Format("{0} {1}", First_Name, Last_Name); }
}
and you can simply display it in the view:
#Html.DisplayFor(model => model.CustomerName)
or just bind directly to the value in your own markup:
<span>#Model.CustomerName</span>
(In this approach you might also write some JavaScript to update the client-side displayed value as the values in the other fields change.)
It really depends on what you want to do with this field, if it saves back to the model or is only for display purposes.
Related
So I am developing my first webpage using ASP.NET MVC and I managed to create a fully working registration page which send the data to the database and stored the user. Simple.
However I didn't really like the look and feel of the element it created for me so I thought I could change it out.
Original code WORKING
<div class="form-group">
#Html.LabelFor(model => model.Firstname, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Firstname, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Firstname, "", new { #class = "text-danger" })
</div>
</div>
My new code NOT WORKING
<div class="row">
<div class="input-group bb-none">
<i class="fas fa-address-card"></i>
<input type="text" placeholder="Firstname">
</div>
</div>
I am 99.9% sure that it's a binding issue. I want the data I put into my new textbox
<input type="text" placeholder="Firstname">
To carry over the data to the model.
What's the part that binds it in the first option?
Tag helpers will resolve down to html and put the property name as both the id and name within the input. The model binder then binds to that.
#Html.TextBoxFor( m => m.Firstname, new { placeholder = "Firstname" })
I have a textbox in a MVC project that prompts a user to enter a date as mmddyyyy. I have the code set up so that the user can only input numbers (ie, no "/"s or "-"s). I need to then convert this data to yyyy-mm-dd to ensure that correct data is being added to the database once the form is submitted.
I realize I will probably need to use DateTime.Parse to do this, but I can't for the life of me figure out how to add it to the following code:
<div class="form-group">
#Html.LabelFor(model => model.Buy2IDExpireDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.Buy2IDExpireDate, new { id = "coBuyerIDExpireDate", #class = "form-control" })
#Html.ValidationMessageFor(model => model.Buy2IDExpireDate, "", new { #class = "text-danger" })
</div>
</div>
Would it look something like this?
string str = Date.Parse(Buy2IDExpireDate).ToString("yyyy-MM-dd");
If so where do I put it in the above code, and do I need to write extra code to ensure that the newly formatted date is stored by the database?
You can add value attribute like this:
#Html.TextBoxFor(model => model.Buy2IDExpireDate,
new { #Value = Model.Buy2IDExpireDate.ToString("yyyy-MM-dd") })
I have a text area where users have to write a description. I was wondering how to set a text box so that it is on multiple lines, allowing the user to see what they are writing. What I am looking for is similar to the environment that I am writing this question in.
<div class="form-group">
<div class="col-md-10">
#Html.EditorFor(model => model.Title, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
In your model you need to add MultilineText as below:
[DataType(DataType.MultilineText)]
public string Title { get; set; }
Or you code have just changed the EditorFor to TextAreaFor like Below
#Html.TextAreaFor(model => model.Title, new { htmlAttributes = new { #class = "form-control" } })
Currently I have a view which takes an IEnumerable model which I use to display data on the view. However on the same view I also have a modal popup in which I want to add to the model rather than separating them out into different views. I tried to follow the suggestion at the bottom of this question How to access model property in Razor view of IEnumerable Type? but got an exception
The expression compiler was unable to evaluate the indexer expression '(model.Count - 1)' because it references the model parameter 'model' which is unavailable.
At the top of the view I have
#model IList<Test.Models.DashModel>
and within my modal body I have
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>DashboardModel</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model[model.Count - 1].DashName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model[model.Count - 1].DashName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model[model.Count - 1].DashName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model[model.Count - 1].CreatedDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model[model.Count - 1].CreatedDate, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model[model.Count - 1].CreatedDate, "", new { #class = "text-danger" })
</div>
</div>
</div>
}
I agree that overuse of the word Model, i.e. the #model keyword, the Model instance variable of the same type, and the default name model given to the lambda parameter name of the HtmlHelper methods is really confusing.
Unfortunately model in this case is the parameter passed by the Html.*For extension methods into your lambda. IMO the scaffolded views could have chosen a less conflicting parameter variable name for the lambdas, e.g. m or x etc.
To access the actual ViewModel instance passed to the view (i.e. #model defined at the top of your razor .cshtml, viz #model IList<Test.Models.DashModel>), what you want to do is to access Model (note the case difference):
#Html.LabelFor(model => Model.Last().CreatedDate, ...
I would also recommend using the Linq extension methods such as Last() / First() etc rather than using the array indexers.
Out of interest, you can of course change the parameter name to anything you like, e.g.
#Html.LabelFor(_ => Model.Last().CreatedDate, ...
Never had this problem before but it is happening on a single MVC action, all other model properties are being passed to the action method but ONE is omitted.
In the BilledItemViewModel model the property that exhibits the problem is declared as:
[Required]
[Display(Name="Factura")]
public int BillId { get; set; }
all other properties work fine but that one above does not.
The action method signature:
[HttpPost][ValidateAntiForgeryToken]
public ActionResult AddBillItem([Bind(Include = "BillId,ItemCodeId,Quantity,UnitPrice,Notes,TaxPercent,IsDebit,Description")] Models.BilledItemViewModel bitem)
The property is being displayed on the View as follows:
<div class="form-group">
#Html.LabelFor(model => model.BillId, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="col-sm-4 input-group">
<span class="input-group-addon">#</span>
#Html.EditorFor(model => model.BillId, new { htmlAttributes = new { #class = "form-control", disabled = "disabled" } })
#Html.ValidationMessageFor(model => model.BillId, "", new { #class = "text-danger" })
</div>
</div>
</div>
When debugging I set a breakpoint on the action and it hits, it also says Model State IS valid. When I examine the values of the passed object (bitem) I can see all properties have the values I entered in the form EXCEPT BillId which is ZERO rather than ONE.
The problem is that your BillId is disabled in your view. Disabled fields are not sent to the server. Either just enable it for editing or make it a hidden input.
#Html.HiddenFor(h => h.BillId)
Hidden input will not be visible for users but will be sent to the server as well as the other fields from your form.
You can mark it as readonly as well.
Readonly controls will be sent to server.