我有这个表单,当用户输入内容时,会将其作为finalAnswer
提交,如下所示:
<g:form controller="animais" params="['rootNode': rootNode, 'finalAnswer': finalAnswer]">
<h3>${curQuestion} </h3>
<g:if test="${finished}">
<g:actionSubmit class="btn btn-primary" action="backToStart" value="Voltar" />
</g:if>
<g:if test="${!finished}">
<g:if test="${!showDivTip}">
<g:textField name="finalAnswer" value="${finalAnswer}"/>
<g:actionSubmit class="btn btn-primary" action="submitFinalAnswer" value="OK" />
</g:if>
<g:if test="${showDivTip}">
<g:textField name="tipToFinalAnswer" value="${tipText}"/>
<g:actionSubmit class="btn btn-primary" action="submitTipForAnswer" value="Finalizar" />
</g:if>
</g:if>
</g:form>
问题是finalAnswer
作为2个字符串数组来到params
,即使内容是一个简单的字符串。
当我打印params
时:
[finalAnswer:[, leao marinho], rootNode:2, _action_submitFinalAnswer:OK, action:index, format:null, controller:animais]
params.finalAnswer
的价值:[, leao marinho]
params.finalAnswer
的ClassType:class [Ljava.lang.String;
如何将params.finalAnswer
检索为简单字符串?
答案 0 :(得分:1)
试试这个
<g:form controller="animais" params="['rootNode': rootNode]">
<h3>${curQuestion} </h3>
<g:if test="${finished}">
<g:actionSubmit class="btn btn-primary" action="backToStart" value="Voltar" />
</g:if>
<g:if test="${!finished}">
<g:if test="${!showDivTip}">
<g:textField name="finalAnswer" value="${params.finalAnswer}"/>
<g:actionSubmit class="btn btn-primary" action="submitFinalAnswer" value="OK" />
</g:if>
<g:if test="${showDivTip}">
<g:textField name="tipToFinalAnswer" value="${params.tipText}"/>
<g:actionSubmit class="btn btn-primary" action="submitTipForAnswer" value="Finalizar" />
</g:if>
</g:if>
</g:form>
答案 1 :(得分:1)
@quindimildev回答不完全正确。更好的是使用隐藏字段:
public class LocationController : MasterController
{
private ROCKWELLTESTEntities db = new ROCKWELLTESTEntities();
//
// GET: /Master/Location/
public ActionResult LocationIndex(string text)
{
if (TempData["msg"] != null)
{
ViewData["error"] = TempData["msg"];
text = null;
}
IEnumerable<location_master> locms;
if (text == null)
{
locms = db.location_master.ToList().Where(l => l.LOC_DEL.Equals(0));
}
else
{
locms = db.location_master.ToList().Where(l => l.LOC_DEL.Equals(0) && l.LOC_NAME.Contains(text));
}
return View(locms);
}
public ActionResult Edit(int id)
{
location_master locms = db.location_master.Find(id);
return PartialView(locms);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(location_master locms)
{
return editProcessDialog(locms, "Location Updated");
}
ActionResult editProcessDialog(location_master locms, string message)
{
if (ModelState.IsValid)
{
if (locms != null)
{
db.Entry(locms).State = EntityState.Modified;
db.SaveChanges();
ControllerContext.HttpContext.Response.Write(string.Format("<div data-dialog-close='true' data-dialog-result='{0}'/>", message));
return PartialView("Edit");
}
else
{
ModelState.AddModelError("", string.Format("Location Not Exist"));
}
}
return PartialView(locms);
}
}
所以你可以从params中删除它。真的,它更清楚。