Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

c# - Query string param is missed when form validation fail

I have an form with following url: CreateEntity?officeCodeId=5

When I send form to validate and if validation is fail it returns just CreateEntity url. No officeCodeId=5.

if user click enter on URL or F5 - my site fail - it require missing officecodeId param. I can save it to the session or in the other storage. But I want to have it in the URL

My view:

[HttpGet]
        public virtual ActionResult CreateEntity(int? officeCodeId)
        {            
            var model = new CreateViewModel();
            FillViewModel(model, officeCodeId);
            return View("Create", model);
        }


[HttpPost]
protected virtual ActionResult CreateEntity(TEditViewModel model)
        {
            if (ModelState.IsValid)
            {
              //Do some model stuff if 
            }

            return View("Create", model);
        }

EDIT. My View:

using (Html.BeginForm("CreateEntity", "Employee", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.HiddenFor(x => x.OfficeCodeId)  
<div>
                @Html.LabelFor(model => model.FirstName, CommonRes.FirstNameCol)
                @Html.TextBoxFor(model => model.FirstName, Model.FirstName)
                @Html.ValidationMessageFor(model => model.FirstName)
            </div>
            <div>
                @Html.LabelFor(model => model.LastName, CommonRes.LastNameCol)
                @Html.TextBoxFor(model => model.LastName, Model.LastName)
                @Html.ValidationMessageFor(model => model.LastName)
            </div>
<div> <div class="input-file-area"></div>
                    <input id="Agreements" type="file" name="Agreements"/>
</div>   

}

Edit 2. Adding:

@using (Html.BeginForm("CreateEntity", "Employee", FormMethod.Post, new { officeCodeId = Model.OfficeCodeId, enctype = "multipart/form-data" }))

Haven`t help. It produce the following form:

<form action="/PhoneEmployee/CreateEntity" enctype="multipart/form-data" method="post" officecodeid="5">

Solution Is

<form action="@Url.Action("CreateEntity", "Employee")[email protected]" enctype="multipart/form-data" method="post">
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem is your HttpPost action doesn't have any notion of an id parameter. If you want to support a similar URL then make the action signature support that parameter e.g.

[HttpGet]
public ActionResult CreateEntity(int? officeCodeId)

[HttpPost]
public ActionResult CreateEntity(int officeCodeId, EditViewModel model);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...