gpt4 book ai didi

asp.net-mvc-3 - html.TextBoxFor 和 html.Textbox,POSTing 值,参数中的模型

转载 作者:行者123 更新时间:2023-12-05 00:02:15 25 4
gpt4 key购买 nike

好吧,伙计们,需要一些帮助!

我正在使用 asp.net mvc3 razor(我对它很陌生,但做了很多网络表单)

好的,问题来了

我的问题围绕提交 View 。
我有一个非常复杂的模型,我的观点基于(强类型)。

我想将模型返回到 Controller 的 HttpPost 方法中的参数中。基本上做:

public ActionResult Personal()
{
DataModel dataModel = new DataModel();
FormModel model = new FormModel();
model.candidateModel = dataModel.candidateModel;
model.lookupModel = new LookupModel();

return View(model);
}

[HttpPost]
public ActionResult Personal(FormModel formModel)
{
if (ModelState.IsValid)
{
//stuff
}
return View(formModel);
}

现在...
我在 post 方法的 formModel 参数中获取值时遇到问题。

这个作品 (意味着我可以看到值)但是很乏味,因为我必须在每个字段中准确地写出它在字符串中的位置:
@Html.TextBox("formModel.candidateModel.tblApplicant.FirstName", Model.candidateModel.tblApplicant.FirstName)

它呈现如下:
<input name="formModel.candidateModel.tblApplicant.FirstName" id="formModel_candidateModel_tblApplicant_FirstName" type="text" value="Graeme"/>

这不起作用:
@Html.TextBoxFor(c => c.candidateModel.tblApplicant.FirstName)

它呈现如下:
<input name="candidateModel.tblApplicant.FirstName" id="candidateModel_tblApplicant_FirstName" type="text" value="Graeme"/>

现在我假设问题在于 id 的差异

所以请回答我这个:
  • 我这样做是否正确
  • 为什么 textboxfor 没有获得正确的值/id,我如何让它获得正确的值/id,以便我可以在 POST 中检索它(如果这甚至是问题)?
  • 此外,似乎 textboxfor 是限制性的,如果您有日期时间,您如何使用 .toshortdate() 方法?这让我觉得 textboxfor 对我没有用。


  • 快速澄清:
    当我说 textboxfor 不起作用时,它在我获取表单时正在获取值。所以他们填写,但在POST/提交时,我在参数的formModel中看不到它们。

    另一边注:
    没有一个 html 助手工作,这就是问题所在。它们也没有出现在模型状态中。

    感谢大家的帮助

    回答:
    html.TextBoxFor and html.Textbox, POSTing values, model in parameters

    在我看来这是一个问题,我用这个答案中的代码片段替换了所有代码并且它起作用了。

    再次感谢你

    最佳答案

    Am i going about this the right way



    是的。

    Why doesn't textboxfor get the right value/id, and how do i make it get the right value/id so i can retrieve it in a POST(if that is even the problem)?



    您的代码中还有其他东西使这不起作用。很难说,因为您还没有显示所有代码。这是一个完整的工作示例,它说明并证明您的代码还有其他问题:

    模型:
    public class FormModel
    {
    public CandidateModel candidateModel { get; set; }
    }

    public class CandidateModel
    {
    public Applicant tblApplicant { get; set; }
    }

    public class Applicant
    {
    public string FirstName { get; set; }
    }

    Controller :
    public class HomeController : Controller
    {
    public ActionResult Index()
    {
    return View(new FormModel
    {
    candidateModel = new CandidateModel
    {
    tblApplicant = new Applicant
    {
    FirstName = "fn"
    }
    }
    });
    }

    [HttpPost]
    public ActionResult Index(FormModel formModel)
    {
    // the username will be correctly bound here
    return View(formModel);
    }
    }

    看法:
    @model FormModel
    @using (Html.BeginForm())
    {
    @Html.EditorFor(c => c.candidateModel.tblApplicant.FirstName)
    <button type="submit">OK</button>
    }

    Additionally, it seems that textboxfor is restrictive, in the manner that if you have a date time, how do you use the .toshortdate() method? This makes me think textboxfor isn't useful for me.



    我同意 TextBoxFor是限制性的。这就是为什么我建议您始终使用 EditorFor而不是 TextBoxFor .它将允许您使用 [DisplayFormat] 简单地装饰您的 View 模型属性。属性和瞧。你可以得到任何你喜欢的格式。

    例如:
    public class MyViewModel
    {
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime CreatedAt { get; set; }
    }

    并在 View 中:
    @model MyViewModel
    @Html.EditorFor(x => x.CreatedAt)

    将完全按照您的预期格式化日期。

    关于asp.net-mvc-3 - html.TextBoxFor 和 html.Textbox,POSTing 值,参数中的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8194199/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com