Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 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 Reply

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...