L
L
lena_shevtsova2021-11-25 22:04:15
ASP.NET
lena_shevtsova, 2021-11-25 22:04:15

Sending data from a form?

In an mvc project, I want to implement a search, and the results should be displayed in the block below, and not on another page.
the search form is in partial view, the list of found items is too.

search form:

<div>
    @using (Ajax.BeginForm("_RealtyItemsList", new AjaxOptions { UpdateTargetId = "results" }))
    {

        <select class="form-select" id="realty-type" aria-label="Default select example" name="RealtyType" style="width:100px">
            <option value[email protected]>@realtyStore.Models.Types.APARTMENT</option>
            <option value[email protected]>@realtyStore.Models.Types.ROOM</option>
            <option value[email protected]>@realtyStore.Models.Types.HOUSE</option>
            <option value[email protected]>@realtyStore.Models.Types.BED</option>
            <option value[email protected]>@realtyStore.Models.Types.GARAGE</option>
            <option value[email protected]>@realtyStore.Models.Types.WAREHOUSE</option>
            <option value[email protected]>@realtyStore.Models.Types.BUILDING</option>
            <option value[email protected]_AREA>@realtyStore.Models.Types.TRADING_AREA</option>
            <option value[email protected]>@realtyStore.Models.Types.PRODUCTION</option>
            <option value[email protected]>@realtyStore.Models.Types.OFFICE</option>
        </select>
        <div class="input-group">
            <p>Цена</p>
            <input placeholder="от" name="minPrice" type="number" />
            <input placeholder="до" name="maxPrice" type="number" />

        </div>
        <div class="input-group" id="square" style="display:none">
            <p>Площадь</p>
            <input placeholder="от" name="minSquare" type="number" />
            <input placeholder="до" name="maxSquare" type="number" />

        </div>
        <div class="checkBox-group" id="numRooms">
            Количество комнат
            <div class="form-check form-check-inline">
                <input class="form-check-input" type="checkbox" value="1" id="flexCheckDefault" name="NumberRoom[]">
                <label class="form-check-label" for="flexCheckDefault">
                    1
                </label>

                <input class="form-check-input" type="checkbox" value="2" id="flexCheckChecked" name="NumberRoom[]" checked>
                <label class="form-check-label" for="flexCheckChecked">
                    2
                </label>
            </div>
        </div>
        <select class="form-select" aria-label="default select example" name="City" style="width:100px">
            @foreach (var c in ViewBag.Cities)
            {
                <option value[email protected]>@c.Name</option>
            }
        </select>
        <input id="search" type="submit" value="Поиск" />
    }

    <div id="results"></div>
</div>

<script src="@Url.Content("~/Scripts/formRent.js")" type="text/javascript"></script>


_RealtyItemList.chtml - partial view where search results should be displayed. the file is in Views/Home, next to the search form
@model IEnumerable<realtyStore.Models.Realty>

@foreach (var r in ViewBag.Realties)
{
    <div class="realtyItem">
        <div class="realtyItem__img">
            <a href="Flat/@r.Id"><img [email protected] alt="photo" /></a>
        </div>

        <div>
            <a href="Flat/@r.Id"><p class="realtyItem__title">@r.Status @r.Type</p></a>
            <p class="realtyItem__desc_mini">
                @if (r.NumberRoom != null)
                {<span>@r.NumberRoom -комн. </span>}
                @r.Type, r.Squre кв.м.,
                @if (@r.Floor != null)
                {<span> @r.Floor /</span>} @r.Floors эт.
            </p>

            <p class="realtyItem__price"> @r.Price руб.</p>
        </div>
    </div>
}


homeController:
while trying to at least go into the method and get the data
[HttpPost]
        protected ActionResult _RealtyItemsList(Realty realty)
        {
            return PartialView(realty);

        }


when trying to submit the form, going to https://localhost:44351/Home/_RealtyItemsList
and an error
Could not find this resource.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) may have been removed, renamed, or temporarily unavailable. Review the following URL and verify that it is entered correctly.

Requested URL: /Home/_RealtyItemsList


I want to ask one more question right away
, so I fill out the table
db.Owners.Add(new Owner { Id = 0, LastName = "Яковлева", FirstName = "Айлин", Patronymic = "Дмитриевна", Phone = "895102345612", Passport = "1420 777799", CityId = 2, Address = "" });
            db.Owners.Add(new Owner { Id = 1, LastName = "Кошелев", FirstName = "Демид", Patronymic = "Матвеевич", Phone = "89997777777", Passport = "1418 774577", CityId = 0, Address = "" });
            db.Owners.Add(new Owner { Id = 2, LastName = "Игнатов", FirstName = "Андрей", Patronymic = "Давидович", Phone = "89301234567", Passport = "1415 713757", CityId = 1, Address = "" });


but the data looks like this
db.Owners.ToList()[0].Id; // =1
 db.Owners.ToList()[1].Id; // =2
 db.Owners.ToList()[2].Id; // =3

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Glebov, 2021-12-02
@GLeBaTi

You have a POST method, and you make an ajax request via GET (by default).
Try adding new AjaxOptions { HttpMethod = "Post" }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question