locked
create multiple model objects in view RRS feed

  • Question

  • User1398839915 posted

    Hello,

    In my view im trying to send the value of @htmltextbox to controller

    based on this value i retrieve the data from database and show the list in the same view.

    My view:

    @model IEnumarable<My model namespace>

    <div>

    @htmltextbox

    </div>

    <div>

    show the data

    </div>

    1.how to use @model  to assign the texbox value to Model class properties? should i create @model1 for my model class?

    2. how to validate my @htmltextbox so that it shows a message to user to enter only number. As my textbox should take only int

    Friday, June 26, 2020 2:50 PM

Answers

  • User1686398519 posted

    Hi Deepulu1984,

    I made an example according to your needs, please refer to it.

    • Regarding data validation, you need to reference "jquery-3.4.1.min.js", "jquery.validate.min.js" and "jquery.validate.unobtrusive.min.js' and use DataAnnotations, and you also need to use "ModelState.IsValid" to judge the model.For more information, please refer to this link.

    Model

        public class test
        {
            public int Id { get; set; }
            public int content { get; set; }
        }

    Controller

            public MVCTestContext db = new MVCTestContext();
            public ActionResult Index()
            {
                return View(db.tests.ToList());
            }
            [HttpPost]
            public ActionResult Index(test test)
            {
                var result =new List<test>();
                if (ModelState.IsValid)
                {
                  result = db.tests.Where(t => t.content == test.content).ToList();
                }
                return View(result);
            }

    Page

    @model  IEnumerable<WebApplication5.Models.test>
    @using WebApplication5.Models
    @{
        var test = new test();
        test = null;
    }
    <h2>Index</h2>
    @using (Html.BeginForm("Index", "Test4"))
    {
        @Html.TextBoxFor(testmodel => test.content)
        @Html.ValidationMessageFor(testmodel => test.content)
        <button type="submit">search</button>
    }
    @if (Model.Count()!=0)
    {
        <table>
            <tr>
                <td>@Html.DisplayNameFor(m => m.Id)</td>
                <td>@Html.DisplayNameFor(m => m.content)</td>
            </tr>
            @foreach(var item in Model)
            {
                <tr>
                    <td>@Html.DisplayTextFor(m => item.Id)</td>
                    <td>@Html.DisplayTextFor(m => item.content)</td>
                </tr>
            }
        </table>
    }
    else
    {
        <p>There is no data.</p>
    }
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

    Here is the result.

     

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 29, 2020 7:20 AM

All replies

  • User1120430333 posted

    You should understand the viewmodel and how to use it in ASP.NET MVC. You can find articles about the VM and how to effectively use it in ASP.NET MVC.

    https://www.dotnettricks.com/learn/mvc/understanding-viewmodel-in-aspnet-mvc

    Friday, June 26, 2020 3:18 PM
  • User1686398519 posted

    Hi Deepulu1984,

    I made an example according to your needs, please refer to it.

    • Regarding data validation, you need to reference "jquery-3.4.1.min.js", "jquery.validate.min.js" and "jquery.validate.unobtrusive.min.js' and use DataAnnotations, and you also need to use "ModelState.IsValid" to judge the model.For more information, please refer to this link.

    Model

        public class test
        {
            public int Id { get; set; }
            public int content { get; set; }
        }

    Controller

            public MVCTestContext db = new MVCTestContext();
            public ActionResult Index()
            {
                return View(db.tests.ToList());
            }
            [HttpPost]
            public ActionResult Index(test test)
            {
                var result =new List<test>();
                if (ModelState.IsValid)
                {
                  result = db.tests.Where(t => t.content == test.content).ToList();
                }
                return View(result);
            }

    Page

    @model  IEnumerable<WebApplication5.Models.test>
    @using WebApplication5.Models
    @{
        var test = new test();
        test = null;
    }
    <h2>Index</h2>
    @using (Html.BeginForm("Index", "Test4"))
    {
        @Html.TextBoxFor(testmodel => test.content)
        @Html.ValidationMessageFor(testmodel => test.content)
        <button type="submit">search</button>
    }
    @if (Model.Count()!=0)
    {
        <table>
            <tr>
                <td>@Html.DisplayNameFor(m => m.Id)</td>
                <td>@Html.DisplayNameFor(m => m.content)</td>
            </tr>
            @foreach(var item in Model)
            {
                <tr>
                    <td>@Html.DisplayTextFor(m => item.Id)</td>
                    <td>@Html.DisplayTextFor(m => item.content)</td>
                </tr>
            }
        </table>
    }
    else
    {
        <p>There is no data.</p>
    }
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

    Here is the result.

     

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 29, 2020 7:20 AM