1

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">&times;</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.

Amir Kian
  • 155
  • 1
  • 10
  • in your view, did you include the model? if so you can use ```asp-for```. Or whatever action you call once submitted can take that address parameter with ```name``` – aaarianme Mar 18 '21 at 03:23
  • I did @iamaaarianme but It just not render Address field correctly, don't know why. – Amir Kian Mar 18 '21 at 04:54
  • why you written like 'model => mod.Address' instead of 'model => Model.Address', still it is not show properly then add in your htmlattribute "Html.TextAreaFor(model => mod.Address, new { class = "form-control ", rows = 5, cols = 20, id = "editor1", Name = "editor1" })" Please note add '@' in each propertly as you write, in comment section here is not allow multiple @ symbol – Ajay2707 Mar 18 '21 at 05:11
  • @Ajay2707 mod refers to Mode[0] – Amir Kian Mar 18 '21 at 05:16

2 Answers2

1

You try with this, It will work

@Html.TextArea("Address",mod.Address, new { @class = "form-control ", @rows = 5, @cols = 20, @id = "editor1" })
SK Yogi
  • 133
  • 10
  • I know also I can use `new { @class = "form-control ", @rows = 5, @cols = 20, @id = "editor1",Name="recive_address" }` but I want to know the problem – Amir Kian Mar 18 '21 at 05:34
  • 1
    @AmirKian problem in binding of model. You are not binding model passed from action but you binding first element of list which you received from action in model. 2nd reason it seems you using some editor on this field so may be that is the cause. – SK Yogi Mar 18 '21 at 05:41
1

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:

  1. The typed TextBoxFor will generate your input names for you. This is usually just the property name but for properties of complex types can include an underscore such as 'customer_name'
  2. Using the typed TextBoxFor version will allow you to use compile time checking. So if you change your model then you can check whether there are any errors in your views."

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

Ajay2707
  • 5,690
  • 6
  • 40
  • 58