Reputation: 313
I get my model name wrongly instead of getting this way
<input class="text-box single-line" data-val="true" data-val-required="آدرس دریافت محصول اجباری میباشد." id="Address" name="Address" type="text" value="">
it renders it this way
<input class="text-box single-line" data-val="true" data-val-required="آدرس دریافت محصول اجباری میباشد." id="mod_Address" name="mod.Address" type="text" value="">
and my model defined this way:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using identity2.Models;
namespace identity2.ViewModels
{
public class OrderIndexViewModel
{
public products Product { get; set; }
public int Count_order { get; set; }
[Required(ErrorMessage = "آدرس دریافت محصول اجباری میباشد.")]
[Display(Name = "آدرس دریافت محصول")]
public string Address { get; set; }
[Display(Name = "سفارش به صورت آفلاین")]
public bool OfflineOrder { get; set; }
[Required(ErrorMessage = "تاریخ دریافت محخول را انتخاب کنید")]
[Display(Name = "تاریخ دریافت محصول")]
[RegularExpression(@"^([1][34]\d{2}\/((1[0-2]|[1-9]))\/(3[01]|[12][0-9]|[1-9]))$", ErrorMessage = "تاریخ معتبر نمیباشد")]
public string recive_date { get; set; }
}
}
and my view:
@model IList<identity2.ViewModels.OrderIndexViewModel>
@using System.Globalization;
@using identity2.Models;
@{
var mod = Model[0];
var dt = DateTime.Now;
PersianCalendar pc = new PersianCalendar();
var timenow = @pc.PearsianDate(dt);
}
<h4>سبد خرید</h4>
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<strong>توجه!</strong> <p>کلیه محصولات در روز پنج شنبه از ساعت 13 الی 19 به صورت رایگان ارسال میگردد در غیر این صورت هزینه پیک بر عهده مشتری می باشد.</p>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<table class="table table-responsive">
<tr>
<th>نام محصول</th>
<th>تعداد سفارش </th>
<th>قیمت کل</th>
<th>عملیات</th>
</tr>
@if (Model != null)
{
foreach (var protoype in Model)
{
var totalprice = @protoype.Product.price * @protoype.Count_order;
<tr>
<td>
@protoype.Product.title
</td>
<td>
@protoype.Count_order
</td>
<td>
@totalprice تومان
</td>
<td>
<a href="@($"/Order/DiscardOrder/{protoype.Product.id}")" )><i class="fa fa-times" aria-hidden="true"></i></a>
</td>
</tr>
}//end foreach
}
</table>
<div class="row">
<div class="col">
<h3>سفارش آنلاین</h3>
@using (Html.BeginForm("CartPaymentPost", "Order", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="row">
<div class="col-md-6">
@if (!ViewData.ModelState.IsValid)
{
@Html.ValidationSummary("", new { @class = "alert alert-warning alert-dismissible fade show" })
}
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => mod.Address, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => mod.Address, new { @class = "form-control ", @rows = 5, @cols = 20, @id = "editor1" })
@Html.ValidationMessageFor(model => mod.Address, "آدرس الزامی میباشد", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => mod.recive_date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => mod.recive_date, null, "recive_date", new { htmlAttributes = new { @class = "form-control", @id = "PersianDate" } })
@Html.ValidationMessageFor(model => mod.recive_date, "تاریخ معتبر نمیباشد!", new { @class = "field-validation-error " })
</div>
</div>
<div class="form-check">
@Html.LabelFor(model => mod.OfflineOrder, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => mod.OfflineOrder, null, "OfflineOrder", new { htmlAttributes = new { @class = "form-check-input" } })
@Html.ValidationMessageFor(model => mod.OfflineOrder, "", new { @class = "field-validation-error " })
</div>
<div class="form-row">
<div class="col col-md-offset-2">
<input type="submit" value="پرداخت" class="btn btn-success" />
@*<a class="btn btn-default" href="/Order/BookPayment/"> <i class="fas fa-shopping-cart"></i> سفارش آفلاین </a>*@
</div>
</div>
}
</div>
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
And it cause a problem that cant bind my fields name to model when I use forms and what I cant figure out is why it just not recognizes address field but there no problem with other fields.
Upvotes: 1
Views: 220
Reputation: 5808
You need to use Html.TextArea
(or write out the <input ...> by hand) instead of the Html.TextAreaFor
.
You need to check the difference between control and controlFor (see below)
"Ultimately they both produce the same HTML but Html.TextBoxFor() is strongly typed where as Html.TextBox isn't.
Generally two things:
Please refer the :
Html.Textbox VS Html.TextboxFor
difference between Html.TextBox and Html.TextBoxFor
ASP.NET MVC 4 override emitted html name and id
Upvotes: 1
Reputation: 143
You try with this, It will work
@Html.TextArea("Address",mod.Address, new { @class = "form-control ", @rows = 5, @cols = 20, @id = "editor1" })
Upvotes: 1